Search Unity

FindWithTag not working

Discussion in 'Getting Started' started by LumoKvin, Dec 10, 2019.

  1. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    FindWithTag doesn't seem to be working on one of my objects. What am I doing wrong?

    upload_2019-12-10_14-49-35.png
    Code (CSharp):
    1. void Start()
    2.     {
    3.         controls = GameObject.FindWithTag("Controls");
    4.         player = GameObject.FindWithTag("Player");
    5.  
    6.         print(controls);
    7.         print(player);
    8.     }
    upload_2019-12-10_14-45-17.png
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @LumoKvin,
    Have you created that tag "Controls" and applied it to an object in Inspector panel?
     
  3. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    Yes. I created the tag and attached it to the GameObject. It doesn't work for some reason. I can't figure it out.

    upload_2019-12-10_15-1-13.png
     

    Attached Files:

  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, it should work. Double-check that the objects have the tags applied.
    EDIT: also make sure that your object is also enabled.
     
  5. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    It is applied, as you can see in the following image:
    upload_2019-12-10_15-7-16.png
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Yeah, but I can replicate your setup here in 10sec and it will work just fine. Either the object is toggled off or something else is wrong with it. But at least your smaller screenshots showed object with tag and the tickbox for enable was also active... hm.

    Try with other Tag name and see if the result is same? Also it shouldn't matter if you have more than one tagged, it will return one of them.
     
    LumoKvin likes this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Verify the Controls GameObject exists in the scene hierarchy and is active at the time you are calling FindWithTag. FindWithTag can't find inactive GameObjects, nor GameObjects which aren't even in the hierarchy. Make sure you're not instantiating this Controls object in some other Start method, where this one just happens to be called before the other one.
     
    LumoKvin likes this.
  8. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    Thank you. I had another script that was turning off the object. I changed that and now it is working. Thanks.
     
    JoeStrout likes this.
  9. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    Thanks. That was the issue. I had another script that was disabling it at a certain time.
     
  10. bdilloughery_mvla

    bdilloughery_mvla

    Joined:
    Sep 22, 2017
    Posts:
    39
    I had a similar issue and after some debugging what I found was:

    I was using using "GameObject.FindWithTag();" twice - once for a health bar, and then for the player. The errors kept showing up mostly for the player not being found, and it was definitely correctly tagged.

    After awhile I figured out that once "GameObject.FindWithTag()" failed to find the first object with tag (the healthbar), it would then automatically fail to find the next "GameObject.FindWithTag()", even though the 2nd one was definitely tagged correctly.

    Weird that the errors were saying the 2nd one wasn't found, even though it was the first one that caused the issue