# Hide all gameobjects

**URL:** https://discussions.unity.com/t/hide-all-gameobjects/791518
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 18, 2020, 2:36pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518 "2020-05-18T14:36:52Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![lz7cjc](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lz7cjc/32/961858_2.png) [@lz7cjc](https://discussions.unity.com/u/lz7cjc)
#### Post date: [May 18, 2020, 2:36pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/1 "2020-05-18T14:36:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PraetorBlue](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/praetorblue/32/613450_2.png) [@PraetorBlue](https://discussions.unity.com/u/PraetorBlue)
#### Post date: [May 18, 2020, 2:40pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/2 "2020-05-18T14:40:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Olmi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/olmi/32/984945_2.png) [@Olmi](https://discussions.unity.com/u/Olmi)
#### Post date: [May 18, 2020, 2:48pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/3 "2020-05-18T14:48:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lz7cjc](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lz7cjc/32/961858_2.png) [@lz7cjc](https://discussions.unity.com/u/lz7cjc)
#### Post date: [May 18, 2020, 3:50pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/4 "2020-05-18T15:50:16Z")

</div>

> [@PraetorBlue](#):
>
> Are we talking about objects in the hierarchy, or an array or list in the inspector for a MonoBehavior?

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

---

<div class="post-metadata">

### Author: ![lz7cjc](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lz7cjc/32/961858_2.png) [@lz7cjc](https://discussions.unity.com/u/lz7cjc)
#### Post date: [May 18, 2020, 3:51pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/5 "2020-05-18T15:51:57Z")

</div>

> [@Olmi](#):
>
> 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.

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

---

<div class="post-metadata">

### Author: ![Olmi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/olmi/32/984945_2.png) [@Olmi](https://discussions.unity.com/u/Olmi)
#### Post date: [May 18, 2020, 6:30pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/6 "2020-05-18T18:30:55Z")

</div>

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

> **[Unity - Scripting API: Object.FindObjectsOfType](https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html)**

> **[Unity - Scripting API: GameObject.FindGameObjectsWithTag](https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html)**

---

<div class="post-metadata">

### Author: ![lz7cjc](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lz7cjc/32/961858_2.png) [@lz7cjc](https://discussions.unity.com/u/lz7cjc)
#### Post date: [May 18, 2020, 6:43pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/7 "2020-05-18T18:43:25Z")

</div>

> [@Olmi](#):
>
> Unity’s manual has a few good examples, and you can find more in Unity Answers with some google fu.
> 
> [https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html](https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html)  
> [https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html](https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html)

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

---

<div class="post-metadata">

### Author: ![davidnibi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davidnibi/32/243542_2.png) [@davidnibi](https://discussions.unity.com/u/davidnibi)
#### Post date: [May 19, 2020, 4:12pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/8 "2020-05-19T16:12:58Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![PraetorBlue](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/praetorblue/32/613450_2.png) [@PraetorBlue](https://discussions.unity.com/u/PraetorBlue)
#### Post date: [May 19, 2020, 4:17pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/9 "2020-05-19T16:17:12Z")

</div>

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”.

---

<div class="post-metadata">

### Author: ![lz7cjc](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lz7cjc/32/961858_2.png) [@lz7cjc](https://discussions.unity.com/u/lz7cjc)
#### Post date: [May 20, 2020, 12:56pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/10 "2020-05-20T12:56:54Z")

</div>

> [@PraetorBlue](#):
>
> 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:

```csharp
 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

```csharp
everything.setactive(false)
gameobject_wanted(true)

```

.

---

<div class="post-metadata">

### Author: ![PraetorBlue](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/praetorblue/32/613450_2.png) [@PraetorBlue](https://discussions.unity.com/u/PraetorBlue)
#### Post date: [May 20, 2020, 1:23pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/11 "2020-05-20T13:23:07Z")

</div>

> [@lz7cjc](#):
>
> 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:
> 
> ```csharp
> 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
> 
> ```csharp
> everything.setactive(false)
> gameobject_wanted(true)
> 
> ```
> 
> .

Try this out:

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

```

---

<div class="post-metadata">

### Author: ![lz7cjc](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lz7cjc/32/961858_2.png) [@lz7cjc](https://discussions.unity.com/u/lz7cjc)
#### Post date: [May 20, 2020, 5:46pm UTC](https://discussions.unity.com/t/hide-all-gameobjects/791518/12 "2020-05-20T17:46:20Z")

</div>

great thanks - will give it a go
