2017-05-14 106 views
0

所以我在我的畫布上放置了6個UI按鈕,它們是「Tier 1」 - 「Tier 6」 我想將所有「Tier 1」 - 「Tier 6」數組作爲按鈕 這樣我就可以通過C#獲取按鈕並將它們存儲在一個數組中

這裏設置的相互作用是我的代碼

// Buildings 
[Header("Buildings")] 
public Button [] buildingTiers; 
public int numOfBuildingTiers = 6; // Number of building tier buttons; 

// ============================================================== 
void Awake() 
{ 
    buildingTiers = new Button[numOfBuildingTiers]; 

    for (var i = 1; i < numOfBuildingTiers; i++) 
    { 
     GameObject _buildingTiers = GameObject.Find ("Tier " + i).GetComponent<Button>(); // This is line 37 
     buildingTiers [i] = _buildingTiers; // This is line 38 
     buildingTiers [i].interactable = false; 
    } 
} 

,我得到這個錯誤

資產/八溴Interactive的資產/代碼/ Main.cs (37,71):錯誤CS0029: 無法隱式轉換類型UnityEngine.UI.Button' to UnityEngine.GameObject」

資產/八溴Interactive的資產/代碼/ Main.cs(38,33):錯誤CS0029: 無法隱式轉換UnityEngine.GameObject' to 型UnityEngine .UI.Button」

回答

1

試試這樣說:

// Buildings 
[Header("Buildings")] 
public Button [] buildingTiers; 
public int numOfBuildingTiers = 6; // Number of building tier buttons; 

// ============================================================== 
void Awake() 
{ 
    buildingTiers = new Button[numOfBuildingTiers]; 

    for (var i = 1; i <= numOfBuildingTiers; i++) 
    { 
     Button _buildingTiers = GameObject.Find ("Tier " + i).GetComponent<Button>(); // This is line 37 
     buildingTiers [i] = _buildingTiers; // This is line 38 
     buildingTiers [i].interactable = false; 
    }  
} 

您正在獲取按鈕組件並將其「存儲」在GameObject變量中。

此外,您只是添加從第1層到第5層的按鈕,爲您解決這個問題。 <=而不是<

+0

這將工作,謝謝! – honor8

相關問題