- Home /
spawned enemies not following player
there is a button in my game that instantiates an enemy prefab. the player in the enemy AI script is also a prefab but there is a copy of the prefab in my scene. I have tested and the enemy worked fine but when I turned it into a prefab and set the player in the AI to the player prefab the enemies just all bunch together in the middle of the map. does anyone know what I might be doing wrong?
Answer by wewewu · Jul 02, 2020 at 07:36 PM
You can just find the player at start in enemy script
Guess something like this. Put this in the Awake or Start function.
PlayerObject = GameObject.FindGameObjectWithTag("Player");
$$anonymous$$ake sure the player has its tag set to Player. When spawned, the enemy prefab will search for a gameobject that has the player tag. If it finds it, it assigns it to the player variable it is looking for.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002329im_/https://answers.unity.com/themes/thub/images/avi.jpg)