簡體   English   中英

Unity3d C# 嘗試訪問腳本中的數組但無效

[英]Unity3d C# Trying to access array in script but in void

我已經嘗試解決這個問題好幾個小時了,但我就是想不通。

我需要訪問數組“空格中的字母“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");
        }

} }

你的代碼中有太多錯誤的東西。

  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

您的腳本中有一些錯誤。
1.有沒有GetComponent可以返回一個游戲對象EVER。 時期。 GetComponent 函數用於獲取附加到 GameObject 的 SINGLE 組件。
2. 話雖如此,GetComponent 也不能​​返回數組。 您要查找的類型GetComponent<GameObject[]>無效。 有效的 GetComponents 調用如下所示

GetComponent<AudioSource>();
  1. 你有一個全局的 GameObjects 數組,稱為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");
        }
} }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM