- Home /
Tracking a game object transform - Why using FindGameObjectWithTag doesn't work?
Hello everybody,
I'm following a tutorial (precisely the survival shooter one), and I am at the stage to implement the NavMesh. Their original script is like this:
Transform _player;
NavMeshAgent nav;
void Start()
{
_player = GameObject.FindGameObjectWithTag("Player").transform;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
nav.SetDestination(_player.position);
}
So far nothing special. I press play and strangely the enemy (I have only one at the moment in the scene) only reaches the initial position pf the player (0,0,0) instead without following it around if it moves. I realized that the player's position of the player is not updated in the _player
field and it stays at 0,0,0.
I tried a different approach: I dragged and dropped the Player's game object onto the property in the UI (I made the property public first and I changed it into a GameObject). In this case it works flawlessly, but at this stage I wonder:
Does FindGameObjectWithTag make a copy of the object instead of returning a reference to the GameObject? Why it doesn't work in the first instance. I use Unity 5 by the way.
Thanks!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
All my NavMeshAgents are tilted 1 Answer
How do i instantiate a Vector3[] ?? 2 Answers