- Home /
Can not Get the Children of Prefab?
There's a prefab contains a child GameObject,and I drag that prefab to the "testObj" field.
here's the script:
public GameObject testObj;
void Start()
{
Debug.Log(testObj.GetComponentsInChildren<Transform>().Length);
}
it display 0, so , it sounds like I can't get the prefab's children component. Is there any way to get it? Or do I have to drag the child's component in the Inspector panel?
PS: How can I write a script with format in this website textfield?
Answer by Arshia001 · Sep 10, 2011 at 09:02 AM
GetComponentsInChildren retrieves the COMPONENTS, not the gameobjects. Does your child gameobject have a component attached?
To answer your second question, use the code sample button above the text field. It's got a bunch of 0's and 1's on it.
I modified some words , but you can try yourself to test it works or not.
Well, now it looks much better. The problem is that you're trying to retrieve the CO$$anonymous$$PONENTS of type TRANSFOR$$anonymous$$, which is basically impossible, because transforms aren't components. You can iterate through child transforms like this (C# syntax, of course):
foreach(Transform t in testObj.transform){...}
I'm hoping you're familiar with foreach.
Your answer
Follow this Question
Related Questions
Problem with prefab parent and children objects that need to be instantiated multiple times. 1 Answer
Trigger in children object activates function in parent 0 Answers
Accessing prefab's children in inspector 1 Answer
Access components in children of a prefab? 5 Answers
Accessing children of instances vs children of original prefab 1 Answer