- Home /
access child of a gameobject
I am using instantiate() to clone a gameobject and have stored the cloned gameobject in a variable named rocketInstance.This new gameObject have three children named marker,starter and plane. I want to store the marker gameObject in a variable named trrigerObject. I have tried the following trrigerObject=rocketInstance.marker;
trrigerObject=rocketInstance.GetComponent("marker");`
Hi, if I have a gameObject 'rat' attached to a $$anonymous$$ainCamera i should; GameObject rat;
void Start(){ rat = ParentGameObject.transform.GetChild (0).gameObject; } ???
Answer by chetanisinanand · Jul 23, 2014 at 06:29 PM
Easiest way would be : Get Child transform using its index , and then get GameObject of that child transform:
GameObject ChildGameObject1 = ParentGameObject.transform.GetChild (0).gameObject;
GameObject ChildGameObject2 = ParentGameObject.transform.GetChild (1).gameObject;
And so on .....
Answer by Imankit · May 28, 2013 at 01:15 PM
just use this.trrigerObject must be GameObject variable
trrigerObject=rocketInstance.GetChild("marker");
Got this error, "$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityEngine.Rigidbody.GetChild'." I am using trrigerObject as a RigidBody variable.
Yes because GetChild does not exist as a method of gameObject....
Answer by SubatomicHero · May 28, 2013 at 01:08 PM
I would attach a script to your marker gameobject like so:
// C#
public GameObject myMarker;
void Start()
{
marker = this.gameObject;
}
// JS
var myMarker : GameObject;
function Start()
{
marker = this.gameObject;
}
Making it public means other scripts can now access this gameobject.
it didnt solved my problem. I am still using
trrigerObject=rocketInstance.marker;
to store marker gameObject into triggerObject variable,but inspector panel still shows triggerObject as empty.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103052im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating prefab at child (spawnlocations are arrays) 2 Answers
How to find and remove the last child of parent? 1 Answer
Checking if object intersects? 1 Answer
prefab question 1 Answer