Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IndexAutoFrangeException:尝试访问游戏对象数组时,数组索引超出范围?_C#_Unity3d_Scope - Fatal编程技术网

C# IndexAutoFrangeException:尝试访问游戏对象数组时,数组索引超出范围?

C# IndexAutoFrangeException:尝试访问游戏对象数组时,数组索引超出范围?,c#,unity3d,scope,C#,Unity3d,Scope,因此,我试图从用FindGameObjectsWithTag初始化的GameObject数组中访问一个元素,但我得到以下错误 “IndexOutOfRangeException:数组索引超出范围。” 当我打印数组的长度时,我得到了3,这是应该的。我怎么修理它 public class selectObject : MonoBehaviour { // Use this for initialization public GameObject[] objects; void

因此,我试图从用FindGameObjectsWithTag初始化的GameObject数组中访问一个元素,但我得到以下错误

“IndexOutOfRangeException:数组索引超出范围。”

当我打印数组的长度时,我得到了3,这是应该的。我怎么修理它

public class selectObject : MonoBehaviour {
    // Use this for initialization
    public GameObject[] objects;
    void Start () {
        GameObject[] objects = GameObject.FindGameObjectsWithTag("isari");  
        Debug.Log (objects.Length);
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Mouse is down");

            RaycastHit hitInfo = new RaycastHit();
            bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
        if (hit) 
        {
            Vector3 position = hitInfo.transform.gameObject.transform.position;
            Quaternion rotation = hitInfo.transform.gameObject.transform.rotation;
            Debug.Log("Hit " + hitInfo.transform.gameObject.name);
            Object.Instantiate (objects[0], position,rotation);

            Object.Destroy (hitInfo.transform.gameObject);

            if (hitInfo.transform.gameObject.tag == "Construction")
            {
                Debug.Log ("It's working!");
            } else {
                Debug.Log ("nopz");
            }
        } else {
            Debug.Log("No hit");
        }
        Debug.Log("Mouse is down");
        } 
    }
}

Start
函数中声明一个本地
objects[]
变量,隐藏字段
objects
。只需从
start
函数中删除
对象
数组的声明即可

你可以试试这个

public GameObject[] objects;
void Start () {
    objects = GameObject.FindGameObjectsWithTag("isari");  
    Debug.Log (objects.Length);
}

Start
函数中声明一个本地
objects[]
变量,隐藏字段
objects
。只需从
start
函数中删除
对象
数组的声明即可

你可以试试这个

public GameObject[] objects;
void Start () {
    objects = GameObject.FindGameObjectsWithTag("isari");  
    Debug.Log (objects.Length);
}