- Home /
How can i expand/collapse all childs under a gameobject ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstantiateObjects : MonoBehaviour
{
public GameObject prefab;
public Terrain terrain;
public float yOffset = 0.5f;
public int objectsToInstantiate;
public bool parent = true;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
//generateObjectOnTerrain();
}
public void generateObjectOnTerrain()
{
for (int i = 0; i < objectsToInstantiate; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
if (parent)
objInstance.transform.parent = this.transform;
}
}
}
When they are parent they are under a GameObject as childs. How can i do in script that when running the game in the hierarchy it will expand the childs or collapse ?
This is when they are all show:
And this is unexpanded
Answer by IMemeManI · May 16, 2017 at 09:55 AM
@Chocolade https://docs.unity3d.com/ScriptReference/EditorGUI.Foldout.html < their's the document on EditorGUI.Foldouts, I also saw somewhere, a Guy used the Collapse
command. You also need to be more clear, do you want to close it from a script or do you just want to close it?
EDIT: I didn't read it proper, to expand it from a script, correct me if I'm wrong but the Collapse
command is the way to go about doing that, I'm still learning scripting so forgive me if I'm not helping much, google, "Unity Collapse Hierarchy" and you'll probably find an expert whom has answered your question.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122115im_/https://answers.unity.com/themes/thub/images/avi.jpg)