Search Unity

Resolved Enabling game objects with tag

Discussion in 'Scripting' started by Redo122_, Aug 4, 2020.

  1. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    I'm having some trouble enabling game objects which are child objects of 'Level 2' with a tag called 'Level 2'.
    This is the code I used:

    Code (CSharp):
    1. GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level 2");
    2. foreach(GameObject go in gameObjectArray)
    3.     {
    4.         go.SetActive (true);
    5.     }
    The code works to disable objects when 'true' is changed to 'false'.
     
  2. ZaffreSheep

    ZaffreSheep

    Joined:
    Aug 26, 2018
    Posts:
    32
    Since it only works for disabling the objects, I'm pretty sure that the FindGameObjectsWithTag() function only finds active GameObjects. Once you set them to false, it can't find the objects to set them to active.

    You could fix this by creating the array as a public variable, and setting the array to all the objects in the Start() function. That way, the array will only be set once, and then it has all the GameObjects stored, even after they're disabled.
     
  3. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    How would I go about doing this?
     
  4. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Code (CSharp):
    1. public GameObject[] gameObjectarray;
    2.  
    3. void Start()
    4. {
    5. gameobject array = Gameobject.FindGameObjectsWithTag("Level 2");
    6. foreach(Gameobject go in gameobject array)
    7. {
    8.   go.SetActive(false);
    9.   //Now you can enable them whenever you want
    10. }
    11. }
    I would get around this by seting all of them active and in the start function find them and disable them. Then you can activate them when ever you want
     
  5. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    That's what it's currently set to do.
     
  6. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    In your start method?
     
  7. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    It was something like that. Your one just gives me errors 'CS1026: )', 'CS1002: ;', and "CS1513: }' on line 6.
     
  8. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Yes that's because I wrote Gameobject instead of GameObject and gameobject array instead of gameObjectarray
     
  9. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    That's solved the errors but the gameobjects still don't activate.
     
  10. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    How do you try to activate them?
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You'd use the same array to loop through and activate them. If you deactivate them, you can't use FindGameObjectsWithTag to find them again. You need to use the previous references to them you got before deactivating them.
     
  12. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    I've figured out what the problem is. My code disables the array right after it's activated so I need to clear it before that.