- Home /
GameObjects static array NullReferenceException
Hello, everyone, I am quite new to Unity and I am trying to port a game I made some time ago in it. I am facing a problem with a static array of GameObjects. I get no errors in the editor, but when I try to access it, I get a NullReferenceException. Here is the code I used to create the array:
 static GameObject[] blockArray = new GameObject[10];
 
     void start() {
         blockArray[0] = GameObject.Find ("Block1");
         blockArray[1] = GameObject.Find ("Block2");
         blockArray[2] = GameObject.Find ("Block3");
         blockArray[3] = GameObject.Find ("Block4");
         blockArray[4] = GameObject.Find ("Block5");
         blockArray[5] = GameObject.Find ("Block6");
         blockArray[6] = GameObject.Find ("Block7");
         blockArray[7] = GameObject.Find ("Block8");
         blockArray[8] = GameObject.Find ("Block9");
         blockArray[9] = GameObject.Find ("Block10");
     }
What am I doing wrong?
Answer by Jessy · Feb 12, 2014 at 07:07 PM
start doesn't get called automatically. Start does.
I recommend using PascalCase for everything except backing fields (which are a stupid concept and shouldn't exist, but that's what we've got), and parameters. If you use camelCase, then you have to change case when converting from a field to a property. Just treat every variable like a property with get and set sharing the access of the property itself, until you want to change one of them.
Also, you can't count on a field initializer on a MonoBehaviour. Move that into Awake.
Thank you, I can't believe I have spent so much time on this and it was because of a lower case letter.
It's not totally your fault; Unity should do a better job of telling you about this without having to resort to debugging.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                