- Home /
How to add component to a child of gameobject
bonjour/hello everyone,
i'm struggling with a basic script : i want to add a collider component to the children of my gameObject. But it's not working; I've been looking for hours and i'm sure it's simple! The purpose of this code is when i enable a toggle script it adds component to the children.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode] //pour executer les evenements
public class ShowCollider : MonoBehaviour
{
public GameObject gOchild;
void OnEnable()
{
int numOfChildren = transform.childCount;//compte le nombre d'enfant du GameObject
int i = 0;
List<GameObject> goChild = new List<GameObject>();
print("Script is Enable, number of children = " + numOfChildren);
for (i = 0; i < numOfChildren; i++)
{
GetComponentInChildren.AddComponent<>(SphereCollider);
//SphereCollider sc = gameObject.AddComponent<SphereCollider>() as SphereCollider; ;
}
}
}
Answer by Cherno · Apr 21, 2018 at 01:47 PM
goChild is empty, you never fill it with any GameObjects.
Here is how you would need to do it:
List<GameObject> childrenList = new List<GameObject>();
//get all transforms in the hierarchy
Transform[] children = GetComponentsInChildren<Transform>(true);
//go through the transform array and only add their gameObjects to the list if they are not the parent (this) gameObject.
for(int i = 0; i < children.Length; i++) {
Transform child = children[i];
if(child != transform) {
childrenList.Add(child.gameObject);
}
}
//go thorugh the list and add the component.
for(int i = 0; i < childrenList.Count; i++) {
SphereCollider sc = childrenList[i].AddComponent<SphereCollider>();
}
Hello,
thank you very much for your help. I'm starting to understand more the logic of unity.
I'm still getting an error at
Transform[] children = GetComponentsInChildren(true);
"impossible to convert a UnityEngine.Transform type to a Transform[] "
i assume its because Transform is an array?
Are you sure you are using the correct code? It's important to use "GetComponent*s*InChildren as opposed to GetComponentInChildren. The former will yield an array with elements that are of type Transform, the latter a single Transform. See also the Scripting API:
https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
Ooooh! You were right! I've forgot the S! Sooorry!!
Answer by Hellium · Apr 21, 2018 at 01:48 PM
void Awake() // Don't use `OnEnable`, otherwise, each time your gameObject is enabled again, the function will run and you may end with several colliders to each children
{
// Compte le nombre d'enfants
int childCount = transform.childCount;
// Ajout un Collider à chaque enfant du gameObject
for (int childIndex = 0; childIndex < childCount ; childIndex++)
transform.GetChild( childIndex ).AddComponent<SphereCollider>();
}
Answer by Clevereen · Apr 22, 2018 at 12:26 AM
I finally found the right syntax of the code! Thank you all!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode] //pour executer les evenements
public class ShowCollider : MonoBehaviour
{
private Transform[] childrenTransform;//déclaration d'un tableau de transform
void OnEnable()
{
/*DECLARATION DES TABLEAUX*/
List<GameObject> childrenList = new List<GameObject>();// création d'une list de type GameObject
childrenTransform = new Transform[transform.childCount]; //get all transforms in the hierarchy
/*DECLARATION DES VARIABLES DE METHODES*/
int numOfChildren = transform.childCount;//compte le nombre d'enfant du GameObject
int i = 0;
print("Script is Enable, number of children = " + numOfChildren);
for (i = 0; i < numOfChildren; i++)
{
childrenTransform[i] = transform.GetChild(i); //récupère la position du GO child et le stock dans le tableau
print("position du child : " + childrenTransform[i]);
if(childrenTransform[i] != transform)
{
childrenList.Add(childrenTransform[i].gameObject);
}
}
//go through the list and add the component
for(i=0;i<childrenList.Count;i++)
{
print("on ajoute : ");
SphereCollider sc = childrenList[i].AddComponent<SphereCollider>() as SphereCollider;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612161230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to control spriterRenderer in childrens 1 Answer
What is the order of execution for the instantiated object's GetComponent.Script.DoSomething? 1 Answer
Is it possible to get the width (in unity units) of a GameObject including all of its children? 1 Answer
Can't add component because class doesnt exist 1 Answer
Recursively get all children 1 Answer