i searched on google, this site and stack overflow but didn´t find a proper soultion of my error.
I want to setActive a specific gameobject in an array true and other false. The Array contains all GameObjects which I want to let check. When i run UseSkin i got this Error message:
NullReferenceException: Object reference not set to an instance of an object
And yes, this method is run, when a button is clicked. UseSkin got the skinID from the button, e.g. 0, 1 or some other numbers. All in range of the array. Array length is 32 (for debuggin only to 2) and the first skin is skins0.
And here is the code snippet
public GameObject[] skins;
public void UseSkin(int skinID)
{
for (int i = 0; i < 2; i++)
{
if (i == skinID)
{
skins[i].SetActive(true);
}
else
{
skins[i].SetActive(false);
}
}
}
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
Are you planning on initializing skins in code? Then don’t make it public. Are you planning on initializing it from the editor? Then make really sure you do.
Problem solved. At first, it was on a wrong gameobject, which got the script in runtime.
After solving this problem I used the right “GetComponentInChildren”. Kurt, I thank you for your support. Sometimes i need a bit space to look on the project, but at the end, everything works.