Accessing instantiated GOs nested in prefabs with GetComponent

I asked this on the forums too in a multi-question post but haven't gotten answer to this yet. So I figured someone else might need this information too and add it to unityAnswers.

So the question, how do I access prefabs that are created with Instantiate and have nested GameObjects.

I'm instantiating the object on runtime with

var house1:GameObject = Instantiate(Resources.Load("HouseGreenSmall"), Vector3(5,-0.15,7), Quaternion.Euler(0,180,0));

I'm not entirely sure if they even are GOs or prefabs. You can see the object in the project view, hierachy view and the script doorlink attached to DoorTrigger GO (or prefab?). I've renamed the Door Trigger to DoorTrigger to avoid any confusion with the space, the screenshot is just from the old version.

I've tried several syntaxes but everything produces some error.

(by the way the accessthis() function is just for debugging)


produces NullReferenceError


works, it prints the correct coordinate, but the coordinate of the GreenHouseSmall prefab.


produces " 'Anything' is not a member of 'UnityEngine.GameObject "

Wow this question is pretty messed up. Sorry for the inconvenience :|

Don't follow your story exactly but I believe you want to find a child of a GameObject/Transform.

The easiest way to do it is through the `Transform.Find` method. So say you have a GameObject named House with a child GameObject named Door. In the script attached to House:

var doorTransform : Transform = transform.Find("Door");
var doorGO : GameObject = doorTransform.gameObject;

You could consider making doorTransform and/or doorGO global variables and setting their values in the Awake function for performance reasons if you need to access the values often. This would result in:

private var doorTransform : Transform;
private var doorGO : GameObject;

function Awake() {
    doorTransform = transform.Find("Door");
    doorGO = doorTransform.gameObject;


now you can just use doorTransform and doorGO in your functions.

in javascript,


should work. Please note that "DoorLink" and "acccesthis" are case sensitive.

PS Prefabs are gameobjects. The only difference between a gameobject in your scene, and a prefab is that one is saved in the scenefile, and the other is saved into a .prefab file in your Assets folder.

In the screen shot, doorTrigger is a child game object of houseGreenSmall and doorlink is a componet of doorTrigger.

so stealing some code from Jaap Kreijkamp's and Lucas Meijer's answers

var doorTransform : Transform = transform.Find("DoorTrigger");

Jaap's first answer does work. I had to experiment with it a bit, though, as Find doesn't work quite as I expected. Apparently it only finds direct children GameObjects, and not, say, grandchildren. :)

I had an imported model asset called anArm, which had nested GameObjects as anArm/skeleton/oneBone/twoBone.

GameObject tmpArm = (GameObject)Instantiate(anArm, thePos, Quaternion.identity);

Given that reference, I first tried this code:

Transform lastBone = tmpArm.transform.Find("twoBone");

It didn't work, either with or without a slash "/twoBone". However, when I included the complete child list:

Transform lastBone = tmpArm.transform.Find("skeleton/oneBone/twoBone");

it worked correctly, and I was able to access the last GameObject. Granted, in the OP, your screenshot doesn't show nesting of more than one level, so the Find should have worked for you. Did it solve your problem?

John C>

"For all your days, prepare, and meet them ever alike;

When you are the anvil, bear - when the hammer, strike."