0

So i have a boolean array with 100 values and 100 buttons , and i want a button to have a color if its respective value in the array it's true and another one if it's false. i tried this :

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

i wrote this because i wanted to add it as a component to the specific button.

i am getting this error NullReferenceException: Object reference not set to an instance of an object butoane.Awake () (at Assets/Scripts/butoane.cs:26)

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • You should have know what NullReferenceException is. Line 26 script has null value. We don't have much info about your editor value and scripts. Also you should format your question. It looks awful. – SeLeCtRa Jan 24 '21 at 17:40
  • Which line gave you `NullReferenceException`. Like actual line because we don't know cause it says line number 26 but I counted 21 lines. Another point, if your colors are hard coded you may want to consider saving it into a constant instead of creating one using `new` every time. – Omar Abdel Bari Jan 24 '21 at 18:03
  • it has no scripts on it, 'lvl' represent the index in the array , i just simply want to get the component to change the color . I used this code to get the text component of a text when the scene loads `txt = this.GetComponent();` , what is wrong if i do the same for a button? – Calin Stefan Jan 24 '21 at 18:03
  • line 26 is : `buton.interactable = false;`, ah thanks for the suggestion with the color constant – Calin Stefan Jan 24 '21 at 18:06
  • That means you have to set buton either in code or in the Unity editor. Sometimes if you rename stuff for example it may no longer be set (the reference to the button remains mapped to the old variable name in that case) – Omar Abdel Bari Jan 24 '21 at 18:08
  • and how can i set the buton in code? – Calin Stefan Jan 24 '21 at 18:11
  • If it's a button you are trying to set then it's unlikely you would want to be setting that in the code. Any reason you can't set that in the unity editor? If instead you are generating the button at runtime then you would be using `new` keyword to generate the buttons. – Omar Abdel Bari Jan 24 '21 at 18:12
  • Ah, i have like 100 buttons i want to put this script on and i figured it would be much easier if i get the component in code than putting it from the editor – Calin Stefan Jan 24 '21 at 18:14
  • If the button is available in your editor before running, you are going to have to set in the editor somewhere (or you could also copy the gameobject id and reference that). Otherwise you have to create it somewhere at runtime and set it to this variable. – Omar Abdel Bari Jan 24 '21 at 18:15
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Omar Abdel Bari Jan 24 '21 at 18:27
  • This question doesn't have anything to do with [tag:unityscript]... – Ruzihm Jan 26 '21 at 21:34

0 Answers0