- Home /
create game object on key press
New to unity, how do I make a game object when I press the space bar through scripting?
I'm kind of lost and I don't know if I'm on the right track.
function Update () {
if(Input.GetButtonUp("Jump"))
(GameObject.Find ("Tnt box"), transform.position, transform.rotation);
}
Answer by skovacs1 · Nov 08, 2010 at 11:59 PM
What's with the commas in the parentheses? I think you missed a function or something.
You should almost never use GameObject.Find because it is slow/expensive. Use GameObject.FindWithTag or in this case, just store the prefab as a variable.
Something like:
var tntBox : GameObject;
function Update() { if(Input.GetButtonUp("Jump")) Instantiate(tntBox, transform.position, transform.rotation); }
Keep in mind that you would be instantiating at the current position with the current rotation. If there are collisions that could happen between the objects, they may be triggered because you are instantiating a GameObject inside of another.