- Home /
Material[] Object reference not set when instantiating
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[i] = aryLukewarmGO[i].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)
Did you debug? - If you didn't, please do and see what's being null (break right before that line gets executed). Either the renderer
or the material
is being null. $$anonymous$$ake sure there's a renderer
attached to your gameObject
, with a material
. Also make sure your arrays are assigned correctly. Are you assigning them somewhere from within your code? If not are you assigning them from the inspector? If not, please do.
Answer by fafase · Sep 13, 2013 at 03:03 PM
You need to set the size of the other array.
private Material[] aryOriginalMaterial;
private GameObject[] aryLukewarmGO;
This creates two reference variables to the type they are, like pointers. But they contain nothing.
aryLukewarmGO = GameObject.FindGameObjectsWithTag("Lukewarm");
The method returns an array, it actually creates it and gives the address to the array reference. So you have your object.
but when you do :
aryOriginalMaterial[i] = aryLukewarmGO[i].transform.renderer.material;
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[size];
But you need to know how big it should be or you use a list:
List <Material> materialList = new List<Material>();
Perfect. I think because the script to fill the GameObject array created it and filled it, I didn't realize I needed to create the array as a separate step.
I added:
aryOriginal$$anonymous$$aterial = new $$anonymous$$aterial[aryLukewarmGO.Length];
before the for loop and it worked like a charm.
Answer by lvictorino · Sep 13, 2013 at 03:02 PM
First, the error tells you that you may want to check if aryOriginalMaterial[i] and aryLukewarmGO[i] 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.
Your answer
Follow this Question
Related Questions
How to Instantiate an Integer Array in an Array of Arrays? 1 Answer
Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers
NullReferenceException with an Array 2 Answers
IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers
Access Multiple Renderer Materials? 1 Answer