- Home /
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?
Thanks
You can also make it var newObject : GameObject; but Transform is recommended to use all the times ins$$anonymous$$d of GameObject, I don't know if it's cheaper, but I know for SURE that's is better.
Depends what you want to do with the instantiated object. If you're just instantiating and not storing the reference, it makes no difference. If you need to manipulate the position a lot, you'd use transform. If you're just going to add components to it, you'd use gameobject
Answer by Tetrad · Jul 01, 2010 at 02:00 AM
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.
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Spawn from array 2 Answers
Instantiate and Scaling 0 Answers
Help, can't get Grid to instantiate in the center of the screen 0 Answers
Don't render prefab until onCollision - related, parent prefab.. 0 Answers