Simple object hierarchy problem

Hi, I have really simple problem but it’s so confusing in UNITY that I just can’t solve it. I have gameobject (lets name it gmo) on scene and inside this gameobject is model exported from 3Dmax (lets name it pig), I have assigned script to ‘gmo’ and i want to rotate ‘pig’ inside ‘gmo’. I tried everything GetComponent(), Find(), hints from this forum. All I get is errors. Is it really so hard in unity to access object in object.

I’m new to unity, I’m a Flash developer and I was very surprised how simple unity is in places where you would think it will be hard and how hard it can be in something as simple as accessing object in object. In flash I would simply write gmo.pig but how to do that here.

for(var t : Transform in transform){
   if(t.name == "childObject"){
       // do your code in here.....
   }
}

thats one way to do it. Another way to do it , would be to create a variable in your parents scripts, of type Transform, and assign the child transform to that variable. You have access that way as well.

You mean foreach loop. I’m doing this:

foreach(Transform t in transform){
if(t.name == “pig”){
print(t.name);
}
}

and it’s not printing anything. BTW in unity I’m using C#

Hello turbo_x.

You’ve done it correctly. If it’s not working, then problem must be somewhere else.

  • Which method did you put this code into ?
  • Is that script for sure attached to the gmo object ?
  • Is the pig object for sure child of gmo object ?

Or maybe we misunderstood you and “pig” is not the GameObject, but just a mesh that is attached to the gmo object? In which case, you’d not have to find child object, but just use translate object directly.

Anyway, there is another way of finding child objects, that doesn’t require iteration over children:

Transform pigObj = transform.Find("pig");

Thanks Luno it was mesh. Problem solved.