Morning Dan,
Its generally when we go to try and use a variable that has been declared, but has not been assigned with anything.
So for an example say we have a movement script attached to a player, and that player object had a Rigidbody component attached as well. And we want to apply a force to the player when it starts.
At the top of our script we would declare a reference for the Rigidbody.
Rigidbody myRigidbody;
If in our Start() method we went to try and use this straight away with
myRigidbody.AddForce();
Intellisense wouldn’t complain as you have declared this as a Rigidbody type, and as a Rigidbody type it would have a method AddForce() and intellisense would recognise this.
The issue would come if you went to run it.
It would declare the variable, and then try and call the AddForce() method on it. At that point you would get a null reference exception.
This has to do with the way variables of an object type are declared and used.
When we declared our myRigidbody variable at the top. As a Rigidbody is an object variables of this type are stored as references.
When a variable is declared that refers to an object, if it hasn’t been assigned anything, its value is defaulted to Null.
Thereby a null reference.
So in this example we would declare our myRigidbody, but at this point its null, so we would have to tell it what its meant to be pointing/refering to with.
myRigidbody =GetComponent()
So now we should be ok as we have declared our reference variable, and assigned it before we try and use it.
So one thing I do as a help if I get any errors like this is to check if a reference variable is null before I use it.
For example in your script we could do a little check after walker is assigned just to make sure it found the component.
Just after the assignment
if (walker == Null)
Debug.Log(“walker had not been assigned”)
Sorry its not much, but I’m sitting on a train on my phone, so typings not the best