Search Unity

beginner unity rpg game question(please help)

Discussion in 'Scripting' started by unity992, Mar 21, 2018.

  1. unity992

    unity992

    Joined:
    Jan 24, 2018
    Posts:
    44
    hi im following a tutorial to make a rpg game.this is part of the code to equip my player with the armor.
    what does this line trying to say "newmesh.transform.parent = bodymesh.transform;"?
    why transform.parent?


    Code (CSharp):
    1. SkinnedMeshRenderer newmesh = Instantiate(newitem.mesh);
    2.         newmesh.transform.parent = bodymesh.transform;
    3.         newmesh.bones = bodymesh.bones;
    4.         newmesh.rootBone = newmesh.rootBone;
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's adding the newmesh as a child to bodymesh? :)
     
  3. Mokzen

    Mokzen

    Joined:
    Oct 10, 2016
    Posts:
    102
    Probably so that the armor follows the actual body of the character? :
     
  4. unity992

    unity992

    Joined:
    Jan 24, 2018
    Posts:
    44
    What is transform.parent? What do i get from the mesh transform's parent?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  6. unity992

    unity992

    Joined:
    Jan 24, 2018
    Posts:
    44
    Yea i read that before asking in this forum.im confused about what it is trying to say.sry im abit slow...

    SkinnedMeshRenderer is a component in a mesh.
    parent of skinnedMeshRenderer's transform.is it talking the mesh itself?
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In the example you posted, it's assigning body mesh as the parent.

    a parent can be another transform or null if there is none.

    If that answers your question, that's good. Otherwise, I'm not clear on what you're asking, sorry.
     
  8. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    I got confused with that too when starting with unity. For all MonoBehaviours (Components) you have Properties to access the main components of the GameObject they belong (This also includes how the GetComponent methods work). so even if the reference is a SkinnedMeshRenderer, newMesh.transform returns the transform of the game object that SkinnedMeshRenderer is attached to. Same with .gameObject.

    Some of this references have been deprecated (rigidbody, renderer) so don't use them.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Let me try to elaborate too.

    A GameObject in Unity is a data structure that includes at least a Transform component, and usually a bunch of other components (in this case a SkinnedMeshRenderer component, probably a MonoBehavior script or two, etc.).

    When you write gameObject in a MonoBehaviour, it returns a reference to the GameObject that your script is part of. When you write transform in a MonoBehaviour, it is exactly the same as gameObject.GetComponent<Transform>() — that is, it returns the Transform component attached to the same GameObject as your script.

    Then of course you probably understand that Transforms have a parent and children, that is, they form a tree structure known as the scene hierarchy (as you can see in the Hierarchy window). So, transform.parent is the transform of the object that contains this one in the scene hierarchy.
     
    whileBreak likes this.
  10. unity992

    unity992

    Joined:
    Jan 24, 2018
    Posts:
    44
    For example there is cube that is a child of an empty gameobject.so the empty gameobject's transform is the parent of the transform of the cube.am i right to say that?is that what u are trying to say
     
  11. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, that's it.