Finding the gameObject that a transform is attached to;

Hi all.
Is there a way to find the gameObject that a transform is attached to ?

I would like to :

C#

GameObject[] gameobjects;
foreach(Transform tr in transform)  
{

gameobjects.Add(tr.gameObject) // << this is obviously not right.

}

Or is there another way of collecting the gameObjects that are children
of a transform;

If you have a reference to a transform, you can just do transform.gameObject to find the GameObject it’s sitting on, and that’s what you’re already showing in your code. In other words, you’re already doing the right thing. :slight_smile:

If you’re having difficulty with this code, it’s because you can’t append things to a built-in array. Either use ArrayList instead of a built-in array, or use transform.childCount to specify the size of the built-in array and just fill it out with references from the foreach loop.