I following tutorial here to make the NPC or enemy character chasing the player but the NPC can't detect where the player is. The player is a prefabs that not placed in the scene, it calls when the game start. So when I'm trying another object in the scene and make it as an object to follow by NPC, the NPC can follow it. Please help me to fix it, I'm new to game development. And because its a multiplayer game, can the NPC choose which player to chase?
Consider checking every now and then if the player is spawned.
You can do this a couple ways.
This post covers a lot of different ways, I would recommend checking every couple frames.
For example you can find any object by it's tag, name, or even stuff like the components it has on it.
One way you could do it is to check for tag for example.
public GameObject PlayerPrefab;
private GameObject actualPlayer;
private void Update()
{
if(actualPlayer != null)
{
FollowPlayer();
}
else
{
actualPlayer = GameObject.FindGameObjectsWithTag(PlayerPrefab.tag).FirstOrDefault();
}
}
void FollowPlayer()
{
// Follow Player Stuff Here
}