Help me to debug this error please :"NullReferenceException: Object reference not set to an instance of an object EnemyMovement.Update () (at Assets/Scripts/Enemy/EnemyMovement.cs:30)"

Hello,
I’ve a bug I can’t solve myself because I’m too new to Unity.
I’m stuck on the Survival Shooter tutorial, on the 7th page (Survival Shooter Training Day Phases - Unity Learn).
When I play the game, the Zombunny keeps walking on the same place and the Player can’t be damaged even when I make him contact the Zombunny.

Here are the 2 errors I get in the console (probably the problem is the same ?):
NullReferenceException: Object reference not set to an instance of an object
EnemyMovement.Update () (at Assets/Scripts/Enemy/EnemyMovement.cs:30)

NullReferenceException: Object reference not set to an instance of an object
EnemyAttack.Update () (at Assets/Scripts/Enemy/EnemyAttack.cs:63)

I’ve copied the code of the script on each page of the tutorial. I’ve just added extra comments to better understand and remember Unity scripting.

Interesting fact, I’ve tried these 2 commands just before 30th line in the script EnemyMovement.cs and they returned different results, which sounds suspicious :
Debug.Log(enemyHealth == null); returns False
Debug.Log(playerHealth == null); returns True
So I think the problem is with playerHealth variable, but I don’t know what I’ve to do.

Please, I need help !


Julien

Either enemyHealth is null or playerHealth is null. First check if there is a GameObject in the scene with the tag “Player”, secondly check if the enemy definitely has a “EnemyHealth” script attached and check if player has “PlayerHealth” script attached. Thirdly, if they do have the respective scripts, check that you are doing a GetComponent on the correct gameObject.

Hello Vintar, thanks for your advices.
I’ve checked all the points you’re talking about, even if I’m not quite sure about the last one. I don’t know how to be sure that the GetComponent is executed on the right GameObject.
Here are some screenshots of the points checked, I’ve made some yellow marks on the interesting details :

As you can see, GameObject Player is tagged “Player” and has its PlayerHealth script

The GameObject Enemy has its 3 scripts including EnemyHealth