【问题标题】:Unity3d C# Trying to access array in script but in voidUnity3d C#试图在脚本中访问数组但无效
【发布时间】:2016-08-19 01:50:39
【问题描述】:

我已经尝试解决这个问题好几个小时了,但我就是想不通。

我需要访问“nextItem”和“prevItem”空隙中的“字母”数组,但我收到一条错误消息,提示“ArgumentException:GetComponent 要求请求的组件 'GameObject[]' 派生自 MonoBehaviour 或 Component 或者是界面。”

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");
        }

} }

【问题讨论】:

    标签: c# unity3d scripting game-engine


    【解决方案1】:

    你的代码中有太多错误的东西。

    1. 声明名为 letters 的游戏对象,然后再次在 Start() 函数中重新声明它。

    2. letters = GetComponent&lt;GameObject[]&gt;();覆盖当前游戏对象引用?

    3. GetComponent&lt;GameObject[]&gt;();你不能这样做。(你的主要错误)

    4. 4.

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

    您无法检查您的数组是否超出范围。 i&lt; 32 应该是 letters.Length

    【讨论】:

      【解决方案2】:

      您的脚本中有一些错误。
      1. 没有GetComponent 可以返回GameObject EVER。时期。 GetComponent 函数用于获取附加到 GameObject 的 SINGLE 组件。
      2. 话虽如此,GetComponent 也不能​​返回数组。您要查找的类型 GetComponent&lt;GameObject[]&gt; 无效。有效的 GetComponents 调用如下所示

      GetComponent<AudioSource>();
      
      1. 您有一个名为letters 的全局游戏对象数组。然后,您将在 Start 中创建另一个数组,这次是在本地创建,将其称为相同的东西 (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");
              }
      } }
      

      【讨论】:

      • 非常感谢,如果我没有一开始就双重声明,我不会有任何问题。在第一个错误之后,我开始弄乱 getComponent 。不过现在已经修好了,谢谢:)
      猜你喜欢
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多