Search Unity

GameObjects and... Sub game objects?

Discussion in 'Scripting' started by The MC, Oct 31, 2006.

  1. The MC

    The MC

    Joined:
    Oct 31, 2005
    Posts:
    105
    Alright, so I've been working on this game for a little while now, but my programmer just graduated (I'm the art guy) so I've been left with both jobs... anyway, I'm trying to give myself a crash course in Unity scripting. So, my question of the moment is, how do I reference (I don't even know what to call them) sub game objects nested within a bigger game object? They're the different named parts that imported from my modeling program. In the screnshot, you can see what I mean. Anyway, each of those little children has different components, like rigidbody, that I wanna affect individually. This may be a really dumb question, and I apologize for wasting anybody's time if it is, but any help would be greatly appreciated, thanks.
     

    Attached Files:

  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Looks like your refencing "children", under their "Parent"
    Good luck and keep going when it drives you crazy
    (I keep telling myself)
    AC
     
  3. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Take a look at http://unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html It gives examples of references other objects.

    If you want to go directly after the components look at http://unity3d.com/Documentation/ScriptReference/GameObject.GetComponentsInChildren.html For example, to do something to the rigid bodies of the children objects
    Code (csharp):
    1.  
    2. var rigidBodies = gameObject.GetComponentsInChildren(RigidBody);
    3. for (var theRigidbody : Rigidbody in rigidBodies)
    4. {
    5.    theRigidbody.AddForce(Vector3.up)
    6. }
    7.  
     
  4. The MC

    The MC

    Joined:
    Oct 31, 2005
    Posts:
    105
    Worked like a charm guys, thanks for the help.