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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[Solved] Easy Question.. Disable Objects With Tag?

Discussion in 'Scripting' started by GhulamJewel, Mar 6, 2015.

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Hi there I am just trying to disable/activate multiple objects with tags. Is this the correct way to do it? Any direction will be appreciative. Thank you.
     
    Last edited: Mar 7, 2015
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    to disable GameObjects with tag "BoxUI":

    Code (CSharp):
    1. void OnMouseDown()
    2.     {
    3.         GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("BoxUI");
    4.  
    5.         foreach(GameObject go in gameObjectArray)
    6.         {
    7.             go.SetActive (false);
    8.         }
    9.     }
     
  3. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Thank you it works great. However now I am using a different button to make it active again in the same scene but sitting it to true and clicking on new button however now does not activate the objects again? Any ideas thank you.

    Code (CSharp):
    1.  
    2.  
    3.         void  OnMouseDown ()
    4.         {
    5.    
    6.                 GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("BoxUI");
    7.                 foreach (GameObject go in gameObjectArray) {
    8.        
    9.                         go.SetActive (true);
    10.        
    11.        
    12.                 }
    13.         }
    14. }
    15.  

    Another question how do I enable/disable scripts on multiple objects with the same name? For example have I have multiple object with a script attached called "spawner" with a button press would like to disable/enable them on all objects that has the "spawner" script. I know how to use

    Code (CSharp):
    1. SpawnObject.GetComponent<Spawner>().enabled = true;
    but this controls one object and script. Thank you.
     
    Last edited: Mar 7, 2015
  4. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    I have solved this bit with this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DisableTouch : MonoBehaviour
    5. {
    6.  
    7.         void  OnMouseDown ()
    8.         {
    9.        
    10.                 GameObject[] gos = GameObject.FindGameObjectsWithTag ("test");
    11.  
    12.                 foreach (GameObject go in gos) {
    13.  
    14.                         go.GetComponent<TouchScript>().enabled = false;
    15.                         go.collider.enabled = false;
    16.  
    17.            
    18.                 }
    19.  
    20.         }
    21. }
    22.  
    But still having trouble reactivating a object after disabling it. Not sure below script not activating disabled object again!

    Code (CSharp):
    1.         void  OnMouseDown ()
    2.         {
    3.          
    4.                 GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("BuildBoxUI");
    5.  
    6.                 foreach (GameObject go in gameObjectArray) {
    7.              
    8.                         go.SetActive (true);
    9.  
    10.              
    11.              
    12.                 }
    13.  
    14.         }
    15.  
    16. }
    I did the below not the most elegant solution....(lol) but it works.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ActivateUI : MonoBehaviour {
    5.  
    6.     public GameObject LevelMenu2D;
    7.     public GameObject Icon;
    8.     public GameObject Buttons;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void OnMouseDown () {
    17.  
    18.         LevelMenu2D.SetActive(true);
    19.         Icon.SetActive(true);
    20.         Buttons.SetActive(true);
    21.  
    22.     }
    23. }
    24.  
     
    Last edited: Mar 7, 2015
    Beefmaster5000 likes this.
  5. MikeBelyayev

    MikeBelyayev

    Joined:
    Jun 4, 2017
    Posts:
    1
    This is an old thread, although came across same issue - UI won't re-activate on go.SetActive(true); Enabling them one by one won't make sense as I got too many. Anyone knows why UI won't set back to active after it was deactivated?
     
  6. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Declare
    Code (CSharp):
    1. gameObjectArray = GameObject.FindGameObjectsWithTag("Tag");
    outside of your if statement,

    in each true/false if statements, put your:

    Code (CSharp):
    1.             foreach (GameObject go in gameObjectArray)
    2.                 {
    3.                     go.SetActive(true);
    4.                  
    5.                 }
    6.  
     
    Redo122_ likes this.
  7. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    I've been having a similar problem to OP. I used davidnibi's code and it's telling me that "The name 'gameObjectArray' does not exist in the current context". While trying to find other solutions I found that I needed to use 'using System;' which didn't fix anything.
     
  8. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Where are you declaring the gameObjectArray array variable?
    Put it right at the start of your code outside of any methods you call (start/update/etc).
     
  9. Annabellica

    Annabellica

    Joined:
    Jan 26, 2017
    Posts:
    1
    Davidnibi's answer is incorrect.
    You need to declare:
    Code (CSharp):
    1. public GameObject[] gameObjectArray;
    - at the very beginning.

    Then make it look for the GameObject with the Tag in the Start() call:
    Code (CSharp):
    1. void Start()
    2.     {
    3.       gameObjectArray = GameObject.FindGameObjectsWithTag ("Tag");
    4.     }
    Then the foreach is going to work
     
  10. GeeGeeJester

    GeeGeeJester

    Joined:
    Dec 8, 2022
    Posts:
    1
    Ok so I used this code to turn a tag false but how would I make it true now?