How do you know what var type when intantiating?

I have seen that sometimes the variable type is Transform, while at other times it is something else. This is a bit confusing because we are really instantiating a GameObject, not just its Transform (or Rigidbody, etc).

var newObject : Transform;
function Update () 
 if (Input.GetButtonDown("Fire1")) 
   Instantiate(newObject, transform.position, transform.rotation);

I get that newObject is an exposed variable and we can set it in the Inspector, but since we are instantiating objects, why is this of type Transform?


I think the original type is just some kind of `Component` that's on the object. Since every objects have a `Transform`, you can use that (and I think some tutorials/documentation use that instead of `GameObject`). You can actually make the variable some other script to limit which prefabs you can drag into that slot in the inspector.

From the documentation:

If the object is a Component or a GameObject then entire game object including all components will be cloned. If the game object has a transform all children are cloned as well. All game objects are activated after cloning them.