Not getting proper reference using Tags

This is continuation of my earlier post here about the stealth project’s CameraMovement functionality. The problem was the camera was’nt moving as expected instead it was just looking downwards at players initial position without following ethen when he was moved using controls. (I have tagged ehtan with Player tag). After I debug what I have found is that this code in CameraMovement was not getting proper reference of actual player but debug statement player.name was printing char_ethan(Clone)

	void Awake ()
	{
		Debug.Log ("Camera Movement: I am awake");
		// Setting up the reference.
		player = GameObject.FindGameObjectWithTag(Tags.player).transform;

		Debug.Log ("Player pos:" + player.position.ToString ());
		// Setting the relative position as the initial relative position of the camera in the scene.
		relCameraPos = transform.position - player.position;
		relCameraPosMag = relCameraPos.magnitude - 0.5f;

		
	}

As per the instructions in the tutorial, player character was positioned at (-2.5,0,0). So the debug statement instead of the assigned values in the inspector, it was printing something else i.e; (0.0, 10.2, 0.0).

I had debug statement inside FixedUpdate() function too to trace the new position of player. But this was also not moving/updating at all and it was printing (0.0, 10.2, 0.0) even though the player object was moving around in the scene.

Similarly I had a debug statement inside PlayerMovement’s awake function to see the initial position

Debug.Log ("PlayerMoveMent.Awake: " + transform.position.ToString ());

This has print correct value (-2.5,0,0). And also the Debug in FixedUpdate() function inside PlayerMovement was priting correct values when I move the player using controls.

I came to the conclusion that the transform reference was not getting actual players gameobject instead it was getting reference of something static object. I tried to find it by its position (0,-10.2,0) but I did not find anywhere in the high level hierarchy.

So I did a tweak inside the CameraMovement. I changed the player variable to public so that I can assign the value from the inspector and commented the player initialization in
CameraMovement.Awake() shown as below

            // Setting up the reference.
		//player = GameObject.FindGameObjectWithTag(Tags.player).transform;

Now the camera is moving as expected. Now the question is what is the mistake I have done that above code was not getting the proper reference of char_ethan? I have checked it is tagged as Player and my Tags.

player is initialized as “Player”. I can’t think of anything right now because I am very new to unity. This is just my 3rd day

Have you checked to see whether any other objects listed in the hierarchy have accidentally been set to use the Player tag? This means that there is a possibility that the first object Unity is finding might not be the ‘real’ player object. A quick way to double check this would be to call GameObject.FindGameObjectsWithTag which finds every game object using this tag, and then you can check the found array to see whether there is more than one object, and which objects they are.

GameObject[] playerObjects = GameObject.FindGameObjectsWithTag(Tags.Player);
foreach(GameObject playerObject in playerObjects)
{
   Debug.Log("Found object: " + playerObject);
}