Search code examples
unity-game-enginetagsgameobjectanimator

GameObject.FindGameObjectWithTag returning (clone)?


im having this problem and its when i use

GameObject.FindGameObjectWithTag("red");

it started when i added an animator component to the gameObject

its returning

red(Clone)

when the game object is in the heirarchy as the only gameobject with the tag "red" it only happened after i added the animator component and when i exited from unity and restarted unity the problem went away, the error i was getting was

MissingComponentException: There is no 'Rigidbody2D' attached to the "red(Clone)" game object, but a script is trying to access it.

there is a red in the scene not a red(clone) but i seen in the inspector when i used GameObject.FindGameObjectWithTag("red") it found red(Clone) but there is no red(Clone) just red in the scene, it happened after i added a animator component and stopped when i rebooted Unity, its gota be a bug, just thought id check here

edit just how i said that there was a red in the scene there was a RigidBody2d attached to it too, just no existence of a red(Clone), thank you for your time


Solution

  • This seems to be a long running bug in unity, dating all the way back to 4.3. Which hopefully will be fixed in unity 5

    It seems the animator window causes a clone for some reason, which is not removable.

    There are only 2 ways to bypass this problem as known to date

    • If you did not save the scene yet, close the animator window, close unity and restart unity. This should rid you of the object.

    in case you already saved the scene

    • Use another tag instead, so instead of red use Red.

    General advice, try to avoid using the FindGameObjectWithTag as long as the bug is running. As stability is not guaranteed.

    Some other less optimal ways to deal with this problem:

    • Find and destroy the clones before using it:

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