Unity3d C# 尝试访问脚本中的数组但无效
Unity3d C# Trying to access array in script but in void
我已经尝试解决这个问题好几个小时了,但就是想不通。
我需要访问数组 "letters in the voids "nextItem" 和 "prevItem",但我收到一条错误消息 "ArgumentException: GetComponent requires that the requested component 'GameObject[]' derives from MonoBehaviour or Component or is an interface."
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");
}
}
}
您的脚本中存在一些错误。
1. 从来没有 GetComponent that can return a GameObject。时期。 GetComponent 函数用于获取附加到游戏对象的单个组件。
2. 也就是说,GetComponent 也不能 return 数组。您要查找的类型 GetComponent<GameObject[]>
无效。有效的 GetComponents 调用如下所示
GetComponent<AudioSource>();
- 您有一个名为
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");
}
} }
您的代码中有太多错误。
声明名为 letters
的游戏对象,然后再次在 Start()
函数中重新声明它。
letters = GetComponent<GameObject[]>();
覆盖当前 GameObject 引用?
GetComponent<GameObject[]>();
你不能这样做。(你的主要错误)
4.
for (int i = 1; i < 32; i++) {
letters[i].SetActive (false);
}
你无法检查你的数组是否越界。 i< 32
应该是 letters.Length
我已经尝试解决这个问题好几个小时了,但就是想不通。
我需要访问数组 "letters in the voids "nextItem" 和 "prevItem",但我收到一条错误消息 "ArgumentException: GetComponent requires that the requested component 'GameObject[]' derives from MonoBehaviour or Component or is an interface."
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");
}
} }
您的脚本中存在一些错误。
1. 从来没有 GetComponent that can return a GameObject。时期。 GetComponent 函数用于获取附加到游戏对象的单个组件。
2. 也就是说,GetComponent 也不能 return 数组。您要查找的类型 GetComponent<GameObject[]>
无效。有效的 GetComponents 调用如下所示
GetComponent<AudioSource>();
- 您有一个名为
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");
}
} }
您的代码中有太多错误。
声明名为
letters
的游戏对象,然后再次在Start()
函数中重新声明它。letters = GetComponent<GameObject[]>();
覆盖当前 GameObject 引用?GetComponent<GameObject[]>();
你不能这样做。(你的主要错误)
4.
for (int i = 1; i < 32; i++) {
letters[i].SetActive (false);
}
你无法检查你的数组是否越界。 i< 32
应该是 letters.Length