我一直试图解决这个问题现在好几个小时,但我只是不知道。我得到一个错误说:“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");
}
} }
非常感谢,如果我没有双倍宣布它在第一位,我不会有任何问题。第一个错误发生后,我开始搞乱getComponent。但它现在已经修复,谢谢:) –