Enemy prefab wont work with the enemy movement script

This game is a 2d game in c#. My enemy is in a prefab and I have a script that spawns the prefab into the game scene, the prefab holds this script and also the enemy movement script, however when the prefab spawns in the scene the player cannot hit the enemy prefab and the enemy will not chase the player, which is what the enemy movement script should do. I know that the enemy movement script works however because when I place an enemy in the game scene hierarchy with the enemy movement script, it chases the player and attacks them, and the player can kill the enemy, it’s just that when the prefabs spawn they don’t do anything and I have no idea why

Please show us the inspector or spawned prefab n probably the enemy script

in your inspector window reference to your player’s transform is missing;
After solving this if your problem still persists create an empty game object in your hierarchy attach your "enemy spawn script " to it.
and then attach the “enemy movement script” to the prefab

now i think it will work fine.

One thing you can do, assuming there is only one object in the scene with a Player script assigned to it, would be to add the following in your Start routine of the Enemy Movement script.


This will find an instance of the Player script somewhere in your scene, and assign it’s transform to your Player variable, on start of the Enemy Movement script.

Hope this helps,

I have the same problem but my enemies dont follow the player can anyone help me?