- Home /
Getting all children of 3d model at once
Attempting to grab all children of my 3d model and make it dissappear at certain times, works as is, but curious if there is a neater way to go about it without declaring all those variables. May need to use this script with 2 components, such as Mesh renderer and a collider. That would requiring 20 variables total and way too messy for my taste. Thanks in advance.
public class DummyDissappear : MonoBehaviour {
public LightFlicker LightFlickerScript;
public Renderer render, render1, render2, render3, render4, render5,render6,render7,render8,render9,render10;
//This Script uses Light Flicker script to make a GameObject dissappear
//May need tweak depending on the GameObject this is applied to
void Start () {
GetComponent<LightFlicker> ();//LightFlicker script
}
void Update () {
if (LightFlickerScript.enabled == false) //LightFlicker script is off
{
render1.enabled = false;//Disable mesh render off all selected children
render2.enabled = false;
render3.enabled = false;
render4.enabled = false;
render5.enabled = false;
render6.enabled = false;
render7.enabled = false;
render8.enabled = false;
render9.enabled = false;
render10.enabled = false;
}
if (LightFlickerScript.enabled == true) //LightFlicker Script is on
{
render1.enabled = true;//Enable mesh render off all selected children
render2.enabled = true;
render3.enabled = true;
render4.enabled = true;
render5.enabled = true;
render6.enabled = true;
render7.enabled = true;
render8.enabled = true;
render9.enabled = true;
render10.enabled = true;
}
}
}
I'm not completely certain I understand the question right, but I'll try anyway. Just disregard it if it's not what you meant.
Why not use a "List" as the type, and add your renderers into in?
That way, you can just do a simple foreach loop, or a Linq request to alter all the objects easily.
Answer by Ricewind1 · Nov 05, 2014 at 02:01 AM
The code below de-activates all children of the mentioned gameobject's transform
foreach (Transform child in transform)
{
child.gameObject.SetActive(false);
}
Or to reference them for future use:
private List<Transform> children;
void Start()
{
children = new List<Transform>();
foreach (Transform t in this.transform)
{
children.Add(t);
}
}
So obvious now, thank you! I tried the foreach and couldn't get it to work before.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170743im_/https://answers.unity.com/themes/thub/images/avi.jpg)