How to tell the children of an object to scale
Hey there, I am pretty new to scripting and am confused by child / parent relationships ....
I am trying to set all of the children of a game objects scale to 0 ... here is my script so far that I am dragging onto an object
using UnityEngine; using System.Collections;
public class ParentChild : MonoBehaviour {
Renderer[] listOfChildren;
void Start(){
listOfChildren = GetComponentsInChildren<Renderer>();
foreach(Renderer child in listOfChildren)
{
//child.enabled = false;
child.transform.localScale = new Vector3(0, 0, 0);
}
}
}
Answer by SneakyLeprechaun · Aug 21, 2017 at 01:59 AM
If the issue is that all objects just disappear instead of only the children, then all you have to do is change the foreach loop to contain
if(child.gameobject != gameobject){ child.tranform.localScale = new Vector3(0, 0, 0); }
Strangely enough, getting the children also gets the parent object as well (there's probably a good reason for this), which means that you'd also be setting the parent scale to 0 as well. So, all you have to do is filter out the parent object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133150im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnMouseDown to activate gameobjects' child 1 Answer
Box collider of a parent prevent the onClick Event of children 0 Answers
Why do we usually put the sprites in the editor as children of another Game Object? 0 Answers
Make object(s) stack vertically. 0 Answers
How to append particle system to GameObjects box collider 0 Answers