NullReferenceException

When I attempt to start the game the enemy doesn’t follow the player and I get the following message:

Welcome to the community @Brando

Something on line 36 in AIController.cs is null. Check what is there that could possible be null, and make sure that you either check if it is null before trying to access it or, if it shouldn’t be null, check why it is

Here’s the code for AIController.cs

From this, it’s only player that can be null. It is possible that the AIController.Start executed before there was a player. You need to check if player is not null

Seems like the player is null. How would I fix this?
Check for null

You’d need to find out why player is null. Does the player game object have the ‘Player’ tag? If it does, why is there no player object in the scene when the AIController.Start() executes?

The player game object has the “Player” tag and it still says it has it when the game is running. Is there something I forgot to put into the player game object?
Player Inspector

I think your issue is here:
image

When you declare GameObject player within a method, the global field player is hidden, which means you’re finding the player and assigning it to the local variable. That variable is discarded as soon as the method ends (immediately) and the original player is still null.
Remove the GameObject from the line so you just have `player = GameObject.FindWithTag(“Player”);

1 Like

I did not even see that. Yep, that would do it.

It works now thanks

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

Privacy & Terms