2016-04-25 66 views
1

我一直试图解决这个问题现在好几个小时,但我只是不知道。我得到一个错误说:“ArgumentException:GetComponent要求请求的组件'GameObject []'从MonoBehaviour或组件派生或是一个接口。”Unity3d C#试图访问脚本中的数组,但在无效

using UnityEngine; 
using System.Collections; 

public class buttons_abc : MonoBehaviour { 
public int id; 
public GameObject[] letters; 

// Use this for initialization 
void Start() { 
    id = 0; 
    GameObject[] letters = GameObject.FindGameObjectsWithTag ("letter"); 
    letters[id].SetActive (true); 
    for (int i = 1; i < 32; i++) { 
     letters[i].SetActive (false); 
    } 

} 
public void nextItem(){ 
    letters = GetComponent<GameObject[]>(); 
    Debug.Log (id); 
    if(id < 32){ 
     letters[id].SetActive (false); 
     letters[id + 1].SetActive (true); 
     id++; 
    } else { 
     Debug.Log("viimane t2ht"); 
    } 
} 
public void prevItem(){ 
    letters = GetComponent<GameObject[]>(); 
    Debug.Log (id); 
     if(id > 0){ 

      letters[id].SetActive(false); 
      letters[id-1].SetActive(true); 
      id--; 
     } else{ 
      Debug.Log("esimene t2ht"); 
     } 

} }

回答

0

有你的脚本的一些错误。
1.有没有GetComponent可以返回一个GameObject如初。时期,GetComponent功能是用于获取单组分nt附加到一个GameObject。
2.也就是说,GetComponent也不能返回数组。您正在寻找的类型GetComponent<GameObject[]>无效。有效GetComponents调用这个样子

GetComponent<AudioSource>(); 
  • 你有GameObjects的全局数组称为letters。然后,您在本地创建另一个阵列,将其称为同一事物(letters)。
  • 全部放在一起

    using UnityEngine; 
    using System.Collections; 
    
    public class buttons_abc : MonoBehaviour { 
    public int id; 
    public GameObject[] letters; 
    
    // Use this for initialization 
    void Start() { 
        id = 0; 
        //Already declared letters globally. Creating another one here locally will mean that your global array will not be populated 
        //GameObject[] letters = GameObject.FindGameObjectsWithTag ("letter"); 
        letters = GameObject.FindGameObjectsWithTag ("letter"); 
        letters[id].SetActive (true); 
        for (int i = 1; i < 32; i++) { 
         letters[i].SetActive (false); 
        } 
    
    } 
    public void nextItem(){ 
        //GetComponent only works on Components. GameObjects are NEVER components 
        //GetComponent cannot return an array 
        //letters = GetComponent<GameObject[]>(); 
        Debug.Log (id); 
        if(id < 32){ 
         letters[id].SetActive (false); 
         letters[id + 1].SetActive (true); 
         id++; 
        } else { 
         Debug.Log("viimane t2ht"); 
        } 
    } 
    public void prevItem(){ 
        //See comment from nextItem() 
        //letters = GetComponent<GameObject[]>(); 
        Debug.Log (id); 
         if(id > 0){ 
    
          letters[id].SetActive(false); 
          letters[id-1].SetActive(true); 
          id--; 
         } else{ 
          Debug.Log("esimene t2ht"); 
         } 
    } } 
    
    +0

    非常感谢,如果我没有双倍宣布它在第一位,我不会有任何问题。第一个错误发生后,我开始搞乱getComponent。但它现在已经修复,谢谢:) –

    1

    太多错误的东西在你的代码。

    1. 声明游戏对象命名letters然后再重新宣布它在Start()功能。

    2. letters = GetComponent<GameObject[]>();覆盖当前的GameObject参考?

    3. GetComponent<GameObject[]>();你不能做到这一点。(你的主要错误)

    4. 4.

    for (int i = 1; i < 32; i++) { letters[i].SetActive (false); }

    你有没有办法来检查,如果你的数组越界。 i< 32应该是letters.Length

    相关问题