- Home /
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?
When the game is playing, does the clone show up in the editor?
No, it does not show up in the editor while the game is playing.
$$anonymous$$aybe at some point you decided to convert your $$anonymous$$ech$$anonymous$$is in a prefab and deleted the original? So this one is considered a Clone, no matter if its the only one in the scene?
The original's still there, according to the editor.
I ran into this same thing today. What causes this issue as I too had (clone)'s of my main character showing up in my "findGameObjectWithTag" results, but nothing in the hierarchy and I know I'm not making clones of my primary character.
For myself, once I quit out of Unity and got back in then it stopped the strange behavior.
So I'm taking it that the above script is primarily for debugging... You find the object that is giving the problem then you can manually kill it?
Is this a Unity bug with some kind of garbage cleanup? I'm just trying to figure out how clones of my main character got created in the first place.
Answer by IgorAherne · Aug 08, 2013 at 10:03 PM
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 :D
Gah! Heisenbug! As soon as I started doing this it went away. Now I can't repro it. Which I guess is a good thing. Cool technique though, that goes in the toolbox.
Answer by Selzier · Oct 23, 2014 at 06:47 PM
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 think this has to do with using a character from the Scene Hierarchy in the Animation preview window to view animation clips.
@Selzier Indeed. I can't reproduce it yet but everytime it happens, it adds another clone if i have the preview window opened
I'm having this problem with the Survival Shooter tutorial project.
I don't know if it's a bug or what, but I'm solving it by selecting the correct instance of the Player without removing the "clone" ones by using:
// player is a private Transform that will be used to get the player position in order to set the enemys destination in the nav system.
foreach (GameObject i in GameObject.FindGameObjectsWithTag("Player")) { if (i.name == "Player") // The player name is this case is Player, so change if necessary player = i.transform; }
Answer by heartofgoldfish · Jul 04, 2015 at 07:30 AM
I've noticed this and reported it as a bug: fogbugz.unity3d.com/default.asp?709676_eil9ufq8jqvd5oks
Answer by meat5000 · May 11, 2016 at 01:34 AM
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 ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613111806im_/https://answers.unity.com/themes/thub/images/avi.jpg)