Hide all gameobjects

Hi
I am currently manually assigning gameobjects in the editor to variables in my script. However the list of gameobjects keeps getting bigger. I need to hide and show a lot of different objects so I would like to hide all gameobjects when my script is called then add the gameobjects i need for the particular section of the game.

Is there a single line i can use to hide everything without having to manually identify each gameobject?
thanks

Are we talking about objects in the hierarchy, or an array or list in the inspector for a MonoBehavior?

If you mean doing that in code, you could just iterate through every item in the array and “hide” them by disabling renderer, for example. So if these objects are on a list, you can hide them all without identifying them yourself.
You could also use tags or the object type (based on a script) to find the objects, and then hide them.

these are objects in the hierarchy - am hoping to avoid lists and arrays! Just want to start with a blank slate each time the script is called and build up based on what i need for that scenario

how would i get the objects into a list or array? Tags sounds interesting if that means assigning all objects to a single group then hiding that group - will research thanks

Unity’s manual has a few good examples, and you can find more in Unity Answers with some google fu.

1 Like

yup I saw them but was hoping for a one line bit of code! Guess I need to put my learning cap on
thanks

If you’re adding them to a list or an array try foreach.
You can get the length of the array or list using Count or Length.

I’m not sure I fully understand what’s happening here, but you mentioned “sections” of your game. I think it would be easiest to put each “section” of the game under a single empty parent GameObject. Then you could simply activate/deactivate those parents to activate/deactivate a whole “section”.

yes, thank you for explaining it better than I had… this is what I am doing but as I increase the number of parent gameobjects i need to update the code:

 public GameObject terrain;
     public GameObject Training;
     public GameObject Signs;
     public GameObject videoStream;
      public GameObject voting;
....
   Signs.SetActive(true);
                Training.SetActive(false);
                terrain.SetActive(true);
                videoStream.SetActive(false);
                voting.SetActive(false);

so instead of having to do all this I would like to

everything.setactive(false)
gameobject_wanted(true)

.

Try this out:

var rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();
foreach (var go in rootObjects) {
  go.SetActive(false);
}
1 Like

great thanks - will give it a go