I’m trying to fill an array of Materials with the materials from an array of GameObjects.
Right now, I have confirmed that GameObject array is filled with the correct gameobjects. I have also confirmed that it gets inside the for loop, but it stops right when I start to instantiate the Material array.
private Material[] aryOriginalMaterial;
private GameObject[] aryLukewarmGO;
aryLukewarmGO = GameObject.FindGameObjectsWithTag("Lukewarm");
for (int i = 0; i < aryLukewarmGO.Length; i++)
{
//Gets here, but stops at next line (next line is line 91 in the error)
aryOriginalMaterial _= aryLukewarmGO*.transform.renderer.material;*_
} In Unity, I get the error: NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) CharacterInput.GoInfrared () (at Assets/Scripts/Player/CharacterInput.cs:91) CharacterInput.Update () (at Assets/Scripts/Player/CharacterInput.cs:42)
The one on the left has no storage location, it is just a null pointer/reference. So either you need to create the array: private Material[] aryOriginalMaterial = new Material; But you need to know how big it should be or you use a list: List materialList = new List();
First, the error tells you that you may want to check if aryOriginalMaterial and aryLukewarmGO exists. Then the problem occurs because your Material array has not been initialized. You may want to use the “new” keyword to instantiate it. I hope it helps, Cheers.