Why UNITY Hates Me By Reading Array's Length!
I'm facing a really annoying error and it forces me to remove every single line that reads arrays length, it sends me this error:
NullReferenceException: Object reference not set to an instance of an object
and for example in a line of a script that reads an array's Length:
public var skidParticles : ParticleSystem[];
public function Update ()
{
if(skidParticles.Length == 0) {skidParticles = new ParticleSystem[1];
skidParticles[0] = Resources.Load("Skid Particle",ParticleSystem);}
}
SO What is wrong with it?! im not able to read the length of that variable?!! can someone help me with this? i will be very much thankful!
Note: Im using [ExecuteInEditMode] In The script!
Answer by PizzaPie · May 02, 2017 at 11:17 AM
Well the array has not be initialized so it can't really check its length. Use:
if(skidParticles == null)
//instead of
if(skidParticles.Length == 0)
Cheers, if there is any compiler errors in this is probably on syntax, i am not familiar with JS.
Thanks So $$anonymous$$uch! It Works Fine Now, You saved me from removing some lines of code ;)