Support Forum

Enemies not following the player

Hi.
I’m new to unity… I’m making a 2D top down shooter game where enemies will follow the player… I implemented the A* pathfinding on my enemy and turned it into a prefab… I then made an enemy spawner script and added the enemy prefab into the script component… But I can’t seem to add the player GameObject into the enemy prefab… I tried to fix the issue by making the player a prefab as well but the enemies only go to one point where the player was… How do I fix this issue? How do I make the spawned enemies follow the player using the pathfinder project? please help me out guys

Hi

You need to assign the actual player object to the target once the enemies have been spawned. This requires a script.

If you assign it in the prefab you will just reference the prefab, not the instantiated object in the scene.

So Basically I can’t use a prefab… But can you give me an idea on how I can achieve my goal using a script? some pseudo codes will do…

Something like

GetComponent<AIDestinationSetter>().target = actualTarget;

I would suggest you read up on some beginner scripting tutorials for Unity to learn more.

Alright man… Thanks a lot for helping me out <3