所以我在画布上放了6个UI按钮,即“第1层” - “第6层” 我想将所有“第1层” - “第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;
}
}
我收到了这个错误
Assets / Octa Interactive的资产/代码/ Main.cs(37,71):错误CS0029: 无法隐式转换类型
UnityEngine.UI.Button' to
UnityEngine.GameObject'
和
Assets / Octa Interactive的资产/代码/ Main.cs(38,33):错误CS0029: 无法隐式转换类型
UnityEngine.GameObject' to
UnityEngine.UI.Button'
答案 0 :(得分: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 变量中。
此外,您只是添加了从Tier1到Tier5的按钮,并为您修复了这些按钮。 <=
代替<
。