My player object is getting clone which doesn't show up in inspector

Some of my code finds the player object with

GameObject avatar = GameObject.FindGameObjectWithTag( “Player” );

lately this code has been returning a different player object than the one in the scene.
Instead of returning “SwingCharMecAlexis” it’s returning “SwingCharMecAlexis (clone)”

Now, as far as I know, I am not instantiating this clone. I don’t know where it’s coming from, and would like to find out, because that’s just weird. I can’t even see it in the editor / inspector while the game is running. So what is this strange semi-visible object? Where did it come from?

I searched for every instance of Instantiate in the code, to see if something was somehow accidentally copying SwingCharMecAlexis that shouldn’t be, but nothing looks like it could cause this.

Is there some other way a (clone) can be created?

Any advice on how I can track this down?

before the code put @ExecuteInEditMode if in java. [ExecuteInEditMode] for c#. If it’s c#, make sure you write using UnityEditor

right after the GameObject.Find instruction of yours, on a new line, put:

Selection.activeObject = avatar;

If you are writing in c#, here is the code:

using UnityEngine;
using System.Collections;
using UnityEditor;


[ExecuteInEditMode]
public class w : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
	GameObject avatar = GameObject.FindGameObjectWithTag( "Player" );	
	Selection.activeObject = avatar;
		
		Debug.Log (avatar);
	}
}

This is an example of the code, you have to stick it in your code, after the avatar was defined.

As soon as you start moving your object with this script in editor window, it will switch to the object with “Player” tag.

Now, when you see it you can voodoo, slap and be strict with him :smiley:

I was having this error about every 5 minutes, depending on what I was working on. For example, when working with Animator Controller, I had to restart Unity almost every time I ran the game. It seems to fix the problem adding the following code to one of the main Player’s Scripts:

GameObject[] remaining = GameObject.FindGameObjectsWithTag(Tags.player);
foreach (GameObject clone in remaining) {
	if(clone.name == "Player_Aleysha(Clone)"){
		GameObject.Destroy(clone);
	}
}

I’ve noticed this and reported it as a bug: fogbugz.unity3d.com/default.asp?709676_eil9ufq8jqvd5oks

My player object as generated by NetworkManager always seems to give a clone. Its also easy to forget to remove the prefabbed object you were just tweaking in the hierarchy, giving a clone. Utilise the hierarchy search. A stray flick of the wrist can lose an object to an unsuspecting new parent :wink: