Search Unity

Rigidbody and Gameobject in Instantiate function

Discussion in 'Getting Started' started by Alobal17, Aug 20, 2019.

  1. Alobal17

    Alobal17

    Joined:
    Aug 2, 2019
    Posts:
    10
    Acrossing the courses of Unity,I find something strange。
    Some time,we can use a Rigidbody Object as a parameter in Instantiate(Object,position,rotation) .
    As usual time ,we use a GameObject Object as a parameter in Instantiate(Object,position,rotation).

    So WHY CAN RIGIDBODY USED TO CREATE AN INSTANCE?
    In my view,Rigidbody only store something about Physics of this gameobject,it isn't include all information about its gameobject,but we still can use gameobject's Rigidbody to instantiate an instance,why?
    Is Rigidbody just as same as GameObject?

    I'm so confused,plz.
    Thank you for your help!!!
     
    rayyanmohiuddin and TSKOKMT like this.
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not quite sure what you are asking. A GameObject is basically a container for attaching various components. A Rigidbody is one of those components which can be attached to a GameObject, and in fact it must be attached to a GameObject.

    If the prefab you are instantiating has a rigidbody component, you can just have your reference to the prefab be to the rigidbody and instantiate and return the rigidbody, which is then attached to a new GameObject. You can see an example of this in the second code example in the documentation below.

    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    This is probably done to just save the step of calling GetComponent<Rigidbody>() on the instantiated GameObject.
     
  3. Alobal17

    Alobal17

    Joined:
    Aug 2, 2019
    Posts:
    10
    Very Thanks!

    Well,so that is if a Component Object is a parameter in Instantiate(Object), Inside the function,it will get the gameobject by the Component,whatever it is Rigidbody or any other,and actually use the gameobject it just have got to make a instance.

    I think I get it!
    Thanks!
    :)