A bunch of Null pointer exceptions and everything else got broken

I’ve been following Rick’s code line by line and I’m getting 2 null pointer exceptions anyway.

If I have all defender objects removed from the scene, I can place only one cactus and no more cactuses or trophys at all - if I try, I’ll get this:
Object reference not set to an instance of an object
DefenderSpawner.AttemptToPlaceDefenderAt (UnityEngine.Vector2 gridPos) which is here:


I’m getting this exception whether I have selected a defender from the defender menu or not.

If I do have a defender game object in the scene, as soon as I start the gameplay and the first attacker spawner spawns an attacker there’s anothe exception: “Object reference not set to an instance of an object Shooter.IsAttackerInLane ()” which is here:

I’ve been breaking my weak brain for the 2nd day with that and I’m stuck. I can’t figure out where my object reference is not set to an instance of an object. Is that the problem in the code or something is messed up in the Inspector?

1 Like

Ok, it seems I figured out the cases.
The first case when I only could place only 1 defender was happening because the 1st defender was decreasing resources to zero and then, there was no way to place any more defenders. It means if my 1st defender doesn’t produce stars, then I go to zero resources right away and that’s it.

The second case was happening when I placed a defender in a lane where the attacker spawner was deactivated. So the IsAttackerInLane method actually needs an attacker spawner to exist to be able to detect its children. As soon as there are all attacker spawners in place, there are no null pointer exceptions.

That said, I’ll dare suggest that the course lectures cover such cases and show how to prevent those exceptions where possible.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms