首页 > 解决方案 > 根据数组统一的值从脚本更改按钮的颜色

问题描述

所以我有一个包含 100 个值和 100 个按钮的布尔数组,如果它在数组中的相应值是真的,我想要一个按钮有一个颜色,如果它是假的,我希望一个按钮有一个颜色。我试过这个:

public Button buton;
public Image imagine;
public int lvl;
void Start()
{
    buton = this.GetComponent<Button>();
    imagine = this.GetComponent<Image>();
}
void Awake()
{
    if(Storage.lvls[lvl] == true || lvl == 0)
    {
        buton.interactable = true;
        imagine.color = new Color(255, 134, 32, 1);
    }
    else
    {
        buton.interactable = false;
        imagine.color = new Color(2, 0, 46, 170/255);
    }
} 

我写this是因为我想将它作为组件添加到特定按钮。

我收到这个错误NullReferenceException: Object reference not set to an instance of an object butoane.Awake () (at Assets/Scripts/butoane.cs:26)

标签: c#unity3d

解决方案


推荐阅读