Thursday, March 14, 2013

Unity: instantiating GameObjects at runtime

Use Prefabs!
Assets > Create > Prefab
Drag the model, material, etc into the new prefab

in the JS script, declare a variable. eg.: var bullet:GameObject;
Assuming that this JS script is added to a GameObject, you can drag a prefab from Project View to the variable in the Inspector view.

instantiate using code: eg.:
var bulletClone = Instantiate(bullet, Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0), Quaternion.identity);


No comments: