尝试从检查器访问动态公共游戏对象数组

时间:2018-02-23 19:52:37

标签: c# arrays unity3d

我正在尝试从检查器中创建一个公共游戏对象变量数组。我有一个问题,理解我应该如何处理使用变量" i"作为数组的索引。我得到一个错误,表示期望值,但我不确定是否应该添加" i"变量到我的索引,例如:ThingToKill[i].SetActive(true);。 我可以让这个工作的唯一方法是在我想要的游戏对象的数字硬编码,所以例如这个ThingToKill[1].SetActive(true);将允许我影响2个游戏对象。

这是我的代码:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class publicarray : MonoBehaviour
   {



public GameObject[] ThingToKill;
public float startAfterThisManySeconds;
public float endafterThisManySeconds;


void Start()
{
    ThingToKill[i].SetActive(false);
    StartCoroutine("turnOn");
    StartCoroutine("turnOff");

    for (int i = 0; i < ThingToKill.Length; i++)
    {
        Debug.Log(ThingToKill[i].name);
    }
}



void Update()
{

}

IEnumerator turnOn()
{
    yield return new WaitForSeconds(startAfterThisManySeconds);
    StartCoroutine("on");
    Debug.Log("on");


}

IEnumerator turnOff()
{
    yield return new WaitForSeconds(endafterThisManySeconds + startAfterThisManySeconds);
    StartCoroutine("off");
    Debug.Log("off");


}
IEnumerator on()
{

    ThingToKill[].SetActive(true);

    yield return new WaitForSeconds(.000001f);



}
IEnumerator off()
{
    ThingToKill[].SetActive(false);

    yield return new WaitForSeconds(.000001f);
}
}

编辑这是有效的解决方案:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class publicarray : MonoBehaviour
{



    public GameObject[] ThingToKill;
    public float startAfterThisManySeconds;
    public float endafterThisManySeconds;


    void Start()
    {

       for (int i = 0; i < ThingToKill.Length; i++)
        {
            Debug.Log(ThingToKill[i].name);
            ThingToKill[i].SetActive(false);
            StartCoroutine("turnOn");
            StartCoroutine("turnOff");
        }
    }



    void Update()
    {

    }

    IEnumerator turnOn()
    {
        yield return new WaitForSeconds(startAfterThisManySeconds);
        StartCoroutine("on");
        Debug.Log("on");


    }

    IEnumerator turnOff()
    {
        yield return new WaitForSeconds(endafterThisManySeconds + startAfterThisManySeconds);
        StartCoroutine("off");
        Debug.Log("off");


    }
    IEnumerator on()
    {
        for (int i = 0; i < ThingToKill.Length; i++)
        {
            ThingToKill[i].SetActive(true);

            yield return new WaitForSeconds(.000001f);

            }
        }

    IEnumerator off()
    {
        for (int i = 0; i < ThingToKill.Length; i++)
        {

            ThingToKill[i].SetActive(false);

            yield return new WaitForSeconds(.000001f);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

ThingToKill[i].SetActive(false);
StartCoroutine("turnOn");
StartCoroutine("turnOff");

并将其放在for-loop内。由于i是循环的局部,因此它会抛出错误,这意味着在它之外它是未定义的。将它放在循环中将解决问题:

void Start()
{
    for (int i = 0; i < ThingToKill.Length; i++)
    {
        Debug.Log(ThingToKill[i].name);
        ThingToKill[i].SetActive(false);
        StartCoroutine("turnOn");
        StartCoroutine("turnOff");
    }
}