- Home /
set public variable for child prefab
I have a prefab which is parent for 4 other prefabs. I would like to set from the parent, a public variable for a child prefab. The way that i would do this is this:
void Start () {
Transform t = transform.FindChild("cube1");
Object texture1 = Resources.Load("jumpButton");
(t as childPrefab).face1Texture = texture1;
}
where "cube1" is a child prefab and face1Texture is a public property of my cube1 prefab.
The thing is that i can't convert with "as childPrefab" so i don't know how to assign my texture to my child prefab.
How can i do so?
Answer by fafase · Sep 10, 2012 at 11:54 AM
GetComponentInChildren is your solution:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponentInChildren.html
If you need to access the four of them:
GetComponentsInChildren
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponentInChildren.html
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to associate prefab with a variable in C#? 1 Answer
Is there a way to set components in a prefab before it is Instantiated? 0 Answers
How do I script a prefab like I would code a class? 1 Answer
Setting attribute in prefab 0 Answers