So my problem is, that I have a GameObject, which spawns enemies, and it has a variable, where I can put my enemy prefab. My only problem is, that my enemy doesn't follow the player.
I made an enemy prefab, with the A* path-finding algorithm, I included the AIDestinationSetter also. The A* works just fine when my enemy is in the scene, but when I try to spawn it, it somehow doesn't seem to know what to do. Any ideas what is wrong?
Thanks for all help, much appreciated!
CodePudding user response:
If the problem is setting the target as the player prefab instead of the player in the scene, add a tag to the player and in the script, get the reference of the player in the scene using
target = GameObject.FindWithTag("Player");
Note: Works only if there is only one game object with the tag "Player" in the scene and if there are multiple of them, this method returns the first one it finds.
To get multiple players as targets, use
targets[] = GameObject.FindGameObjectsWithTag("Player");
For more information, refer: https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html