Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GameObject.FindGameObjectWithTag returning a Clone instead

Discussion in 'Editor & General Support' started by blernsball, Nov 16, 2013.

  1. blernsball

    blernsball

    Joined:
    Nov 15, 2013
    Posts:
    4
    I've been messing around with the new 2D tools and the sample demo project and using them as a reference when creating something new from scratch.

    Here is the issue: I am trying to use the Camera Follow script that the demo project uses. I made a script file, copied the code in, and linked it to my camera. But it doesn't seem to work.

    I did some digging and placed some Debug.Logs in the code and while the script is running, it seems to have an issue with this piece:

    player = GameObject.FindGameObjectWithTag("Player").transform;

    instead of returning the player object (called hero) it returns an object: hero (Clone). That object doesn't appear anywhere in my game and I am not instantiating a clone anywhere.

    quite odd.

    I found this thread with someone having a similar issue:
    http://answers.unity3d.com/questions/510771/my-player-object-is-getting-clone-which-doesnt-sho.html

    I added in the code that was suggested and when I run the game the hero (Clone) object is selected in the Inspector, but nearly everything is greyed out. Nothing is selected in the scene and it doesn't appear in the hierarchy.

    It also doesn't have as many components as the correct hero object does. hero (Clone) only has Transform, Sprite Renderer, and Animator as components, which seems like it might be an early version of the object before I added scripts and colliders, etc. The only thing that is modifiable is sorting layer in the Sprite Renderer component (that component doesn't exist in the correct hero object).

    Anybody have any ideas as to what is going on here? The demo project itself runs as expected.
     
  2. blernsball

    blernsball

    Joined:
    Nov 15, 2013
    Posts:
    4
    Well I have been able to get around the issue by creating a new tag for the player and then searching for that in the code.

    But I did have the issue happen again, I was only able to fix it by reloading without saving.

    I think it may be related to using the animation window, as that is what I was working with when the issue reappeared.

    Another note: When it happens, undo will not fix the issue. The hidden cloned object will remain even after undoing to a point that had previously been ok! Still trying to reliably replicate the issue.
     
    Last edited: Nov 19, 2013
  3. PortableCow

    PortableCow

    Joined:
    Dec 31, 2013
    Posts:
    8
    I've been having the same problem when playing around with the 2D demo project. I came across your post and created a new tag for my player and it fixed the issue. I did notice this after I assigned animations to my player as well. Unfortunately, I'm not sure how to duplicate it either.
     
  4. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    Odd, I just had the exact same thing happen.

    GameObject tutorial = GameObject.FindGameObjectWithTag("Tutorial");

    And it returned a clone of the actual object. I switched to a new tag and then it worked.

    I'm just going to avoid using FindGameObjectWithTag, as I have in the past...
     
  5. beeglebug

    beeglebug

    Joined:
    Oct 20, 2013
    Posts:
    3
    Just wanted to add that this is still happening (4.5.3f3), I was messing with animations on my player, and all of a sudden he gets cloned when the game starts, has this been reported as a bug?
     
  6. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    136
    I am having the same bug right now
     
  7. Code_Monkey_Refurbished

    Code_Monkey_Refurbished

    Joined:
    Jan 7, 2015
    Posts:
    1
    There is no cure for it as far as I know. it is a bug. just create your own tag (ex. "player" in place of "Player") and then all should be copacetic
     
  8. chaneya

    chaneya

    Joined:
    Jan 12, 2010
    Posts:
    416
    This just happened to me in Unity 5.3.0f4 so I'm assuming they still have not fixed the bug. I have Quit and restarted Unity several times and each time I start making changes to the animator, the next thing I know my FindGameObjectWithTag Player tag is no longer recognized returning a null reference. When I debug.log that gameobject upon startup it says it's a clone.
     
  9. deadboltinteractive

    deadboltinteractive

    Joined:
    Oct 31, 2015
    Posts:
    1
    Using 5.3.1f1 - Having the same problem. Seemingly out of the blue, FindGameObjectWithTag is finding a clone which doesn't exist in the hierarchy. Deleted my player out of the scene, and it's still being found. I created a new custom Tag "player", which is still being found as a clone. Bizarre.
     
    Last edited: Jan 15, 2016
  10. AlanGreyjoy

    AlanGreyjoy

    Joined:
    Jul 25, 2014
    Posts:
    192
    Same here.

    It was working fine in 5.2

    Installed the newest Unity, and GameObject.FindGameObjectWithTag only returns with a (Clone)....
     
  11. AlanGreyjoy

    AlanGreyjoy

    Joined:
    Jul 25, 2014
    Posts:
    192
    Well, closing unity and reopening it seems to fix it. So I am happy until the bug is fixed :)
     
  12. dinaga

    dinaga

    Joined:
    Oct 19, 2015
    Posts:
    8
    I am having the same issue with Unity 5.3.4f1...
    Everything works fine for a while, then all of a sudden, "invisible" clones start appearing in GameObject.FindGameObjectsWithTag() results and make programming impossible.

    So far the only solution is to restart Unity, which is a pain. And the clones keep returning after a while anyway.

    Any news regarding this?
     
  13. dinaga

    dinaga

    Joined:
    Oct 19, 2015
    Posts:
    8
    This is a small workaround class I just created, I suppose it can help until Unity gets this fixed.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class UnityHelper {
    6.  
    7.     public static GameObject[] FindGameObjectsWithTag(string tag)
    8.     {
    9.         ArrayList resultList = new ArrayList();
    10.         foreach (GameObject result in GameObject.FindGameObjectsWithTag(tag))
    11.         {
    12.             if (!result.name.Contains("(Clone)"))
    13.             {
    14.                 resultList.Add(result);
    15.             }
    16.         }
    17.  
    18.         GameObject[] resultArray = new GameObject[resultList.Count];
    19.         resultList.CopyTo(resultArray);
    20.         return resultArray;
    21.     }
    22.  
    23.     public static GameObject FindWithTag(string tag)
    24.     {
    25.         try
    26.         {
    27.             return FindGameObjectsWithTag(tag)[0];
    28.         }
    29.         catch
    30.         {
    31.             return null;
    32.         }
    33.     }
    34. }
    35.  
     
  14. KGBemployee

    KGBemployee

    Joined:
    Nov 15, 2015
    Posts:
    3
    I just ran into this issue myself. Is there any word on a fix?
     
  15. getyour411

    getyour411

    Joined:
    Oct 14, 2012
    Posts:
    6
    Just hit the same issue. While I was undocking/watching Animator window my game blew-up on nullRefs and via debug found the player(Clone) symptom. Start and stop of Unity fixed it.