MarK
MarK

Reputation: 69

Access the a child of a GameObject through a script that is assigned to it

I have a problem in making a little game in Unity 5. I have a prefab (named "Controller") and it is instantiated two times in the game. This prefab has a script attached to it, and also has 4 children of type GameObject. How can i access a child to the clone that it is instantiated? I need to change it's layer to Ignore Raycast if a button is pressed, but i don't know how to do it.

Upvotes: 1

Views: 586

Answers (1)

joreldraw
joreldraw

Reputation: 1736

You can do it assigning your instance to a GameObject:

GameObject clon = Instantiate (Resources.Load("MyInstanceObject")) as GameObject;

clon.transform.FindChild("Mychildname").gameobject.layer = index;

or too if you know your child index:

clon.transform.GetChild(index).gameobject.layer = index;

Upvotes: 2

Related Questions