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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Trouble with FindGameObjectsWithTag and SetActive

Discussion in 'Scripting' started by dhernan881, May 28, 2020.

  1. dhernan881

    dhernan881

    Joined:
    May 27, 2020
    Posts:
    3
    Hello, I'm having trouble with using FindGameObjectsWithTag; I want to have two versions of my game based on the boolean
    isHighStakes
    (not included in the code below) and have a set of buttons appear/disappear depending on the version. However, when I try and run this, the high buttons never appear.

    I had the lowStakesVersions and highStakesVersions as private but when I serialized the fields and looked at them in the inspector while the game is running, the lowStakesButtons list is always populated but the highStakesButtons list always says that the size is 0 and never populates.

    I would really appreciate any help! I have the buttons tagged properly but I can provide screenshots in case someone wants to verify.

    Code (CSharp):
    1. void FindButtons(Scene scene, LoadSceneMode mode) {
    2.         lowStakesButtons = new List<Button>();
    3.         highStakesButtons = new List<Button>();
    4.  
    5.         foreach (GameObject highButt in GameObject.FindGameObjectsWithTag("High")) {
    6.             highStakesButtons.Add(highButt.GetComponent<Button>());
    7.         }
    8.         foreach (GameObject lowButt in GameObject.FindGameObjectsWithTag("Low")) {
    9.             lowStakesButtons.Add(lowButt.GetComponent<Button>());
    10.         }
    11.  
    12.         foreach (Button butt in lowStakesButtons) {
    13.             butt.gameObject.SetActive(!isHighStakes);
    14.         }
    15.         foreach (Button butt in highStakesButtons) {
    16.             butt.gameObject.SetActive(isHighStakes);
    17.         }
    18.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    FindGameObjectsWithTag only finds objects which are currently active. If the high stakes objects are inactive then they won't be find. If you need them inactive then create a public List of them somewhere in the scene (even in this script, but doesn't have to be) and drag all of them to the List in the inspector. Use that list of the buttons instead of FindGameObjectsWithTag.
     
  3. dhernan881

    dhernan881

    Joined:
    May 27, 2020
    Posts:
    3
    They are all active when the scene loads; the goal with this function is to set the ones that I don't want active to inactive. I'm trying to use FindGameObjectsWithTag because I want to have this object be around for multiple scenes
     
    Last edited: May 28, 2020
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Well, it seems reasonably clear that this code is doing the same thing with both low and high except for the tag, so if you've verified that one of them is working and the other is not, that strongly suggests the problem is something in how you set up your scene, not in this code.
    Unless all the buttons you are finding also stick around for multiple scenes, note that you will end up with a bunch of references to no-longer-existing buttons, and if you try to do things with them you'll get errors.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Did you know you can also break your scenes up into parts and load them additively? It's pretty powerful.

    In your case it might be these three scenes:

    - base stuff that all share
    - low-stakes stuff
    - high-stakes stuff

    At load time you'd decide which to load.

    Also, try to get away from finding randomly tagged stuff and doing things with it.

    Instead have bespoke items such as "HighStakesController" and "LowStakesController" that have their own static locator functions, so that ALL the logic related to their presence or absence is centralized in that one script, rather than splattered all over your game code like so much glitter. You'll thank yourself later when you have to go change something about it.