NullReferenceException in AIController

So i have tried to follow exactly how it shows in the video but i still get these errors:
NullReferenceException: Object reference not set to an instance of an object

RPG.Control.AIController.InAttackRangeOfPlayer () (at Assets/Scripts/Control/AIController.cs:34)
RPG.Control.AIController.Update () (at Assets/Scripts/Control/AIController.cs:22)

I have solved that the problem is the player that returnes null but the player is tagged as Player and in the script(AIController i got:
player = GameObject.FindWithTag(“Player”);

i am gonna upload a picture of the code.

And here is a picture of the player prefab:

I have read alot of different post thats almost the same problem but cant seem to find my own problem about it.

Im also gonna post enemies prefab:

If there is anything else needed please do respond and try to help. Getting stuck here was a bummer :slight_smile:

Hmmm… this is strange, as I don’t see any red flags in the code… You’ve gotten the Player and the Fighter in Start(), so by Update all should be working properly…
Let’s add in some Debugs…

void Start()
{
     fighter = GetComponent<Fighter>();
     if(fighter==null) 
     {
           Debug.Log($"{name} does not appear to have a Fighter component");
     }
     player = GameObject.FindWithTag("Player");
     if(player==null)
     {
           Debug.Log($"{name} was unable to locate the player!");
     } else
     {
           Debug.Log($"{name} located the player on {player}");
     }
}

Thanks for responding!

So i tried setting in that debug in AIController void start but that did not work, no ide why so i tried doing it in the method InAttackRangeOfPlayer and i got this:
bild_2022-10-14_072006090

and right now my code looks like this:

The fighter script should be ok as i inserted it with namespase using RPG.Combat + i get the component from fighter.

and my enemies got the fighter script attached to them aswell.

The messages are telling you that Enemy(1) and Enemy(2) don’t have a Fighter component… While I see it on the Enemy Prefab you’ve posted, check the actual Enemy(1) and Enemy(2). It’s possible that they do not have the Fighter

Enemy 1 here:

Enemy 2 here:

Zip up your project and upload it to https://gdev.tv/projectupload (be sure to remove the Library folder) and I’ll take a look at it (though it may be tomorrow night after work). I’ll see if I can figure out what’s going on.

No problems im just happy to get some help :slight_smile: i appreciate it, the file should have been uploaded with some other answers and so on. There is no rush check it when you have the time.

It’s the silliest thing… I should have spotted it earlier…
Look at your Start() method in AIController.
All names in C# are case sensitive, so start() is not the same as Start()
Capitalize the first letter of Start() and you should be good to go.

Yea, totally works now… srry about that haha. been trying to solve it for hours and i miss that little thing hehe. Thanks alot!

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

Privacy & Terms