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

Unity 5 "FindGameObjectsWithTag" doesn't work well

Discussion in 'Editor & General Support' started by benzuk, Mar 4, 2015.

  1. benzuk

    benzuk

    Joined:
    Mar 11, 2014
    Posts:
    117
    Hi, I found only bugs on Unity 5. When the game starts, The AI have to find the GameObject of the Player.
    Code (CSharp):
    1.         private Transform GetRandomTarget()
    2.         {
    3.             GameObject[] persons = GameObject.FindGameObjectsWithTag("Player");
    4.             int t = Random.Range(0, persons.Length);
    5.             return persons[t].transform;
    6.         }
    And on unity 4.6 the code works fine, But on unity 5 it is gives me problems. The error:
    I found the way that, But it doesn't work well. The way is to re-click again of the tag which the Player object has.

    Any ideas?
     
  2. benzuk

    benzuk

    Joined:
    Mar 11, 2014
    Posts:
    117
    Someone? Where Unity Support? I can't keep working on my Project.
     
  3. benzuk

    benzuk

    Joined:
    Mar 11, 2014
    Posts:
    117
    GameObject.Find also doesn't work. What the hell happens here?
     
  4. Deleted User

    Deleted User

    Guest

    We'll have a look. Could you please file a bug also? Thanks.
     
  5. benzuk

    benzuk

    Joined:
    Mar 11, 2014
    Posts:
    117
    What is it "file a bug"?
     
    Last edited: Mar 5, 2015
  6. Dreamzle

    Dreamzle

    Joined:
    Apr 23, 2014
    Posts:
    43
    If Unity crashes whenever you open a project, that usually means one or more files got corrupted in your Library folder. So go to your project folder (where Assets and Project Settings are), delete a Temp folder if it exists, and then the Library folder. Note that when you next open the project in Unity, all assets will be reimported, which could take a while if you have a lot.

    As for reporting bugs, in Unity, go to the Help menu at the top and choose "Report a bug...".

    Maybe random.Range(min,max) changed in Unity 5 so that max is inclusive?
     
    Last edited: Mar 5, 2015
  7. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    It doesn't have to be inclusive, maybe there are just no objects with tag "player", so random(0,0) will get you persons[0], which is nonexistent.

    GameObject.Find() should work, too. I would assume that you do this somewhere at level start and your player is not initialized yet.
     
  8. benzuk

    benzuk

    Joined:
    Mar 11, 2014
    Posts:
    117
    Thanks for the answer!
    I talked with Unity Support and them told me It happens because I called the function from Awake function.
    I fixed it.

    Again thanks.
     
    gothicserpent likes this.
  9. RSH1

    RSH1

    Joined:
    Jul 9, 2012
    Posts:
    250
    I'm getting this error when using FindGameObjectsWithTag on iOs from an InvokeRepeating method

    UnityException: Tag: Mob is not defined.

    when it IS defined, and works perfectly well in the editor.
     
  10. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    got the same issue.

    I am calling it from my OnLevelWasLoaded method where it runs perfectly fine in my Editor but not in exe or WebPlayer.

    Then I tried shifting everything into a coroutine that calls this stuff after 1 frame, so everything should be there, right?

    Same! It freaking gives me "Opponent not defined". This bug is so crazy since this method is so important
     
  11. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    Found the solution here

    It comes when you upgrade older projects (in my case from a beta 4 to 5). Your editor will still display the tag of your GO, but when you click "add tag" you will notice that this tag list is empty!!!
    Just re-adding the tag and assigning it EVERYWHERE again solves the problem. Good god, this bug gave me nightmares :D
     
    benzuk likes this.