- Home /
Question by
Wylie · Nov 17, 2010 at 02:26 PM ·
gameobjectinstantiateobject
how to instantiate object when I click
I'm trying to get a 3d object to instantiate whenever a 2d plane is clicked. Here is the code:
var wasClicked: boolean = false;
var redBrick: Rigidbody;
function onMouseOver() {
if(Input.GetMouseButtonDown(0)){
wasClicked = true;
var tempRedBrick: Rigidbody;
tempRedBrick = Instantiate(redBrick, transform.position, transform.rotation);
}
}
The code worked on previous game I was making, but I was instantiating 2d planes with each click. Can anyone give me a hand?
Comment
Best Answer
Answer by denewbie · Nov 17, 2010 at 03:06 PM
1) Use onmousedown event on the play instead 2) Save your GameObject instead of a rigidbody
var redBrick: Rigidbody; var redbrickObj : GameObject; // This is where you store your prefab
function onMouseDown() {
var tempRedBrick: Rigidbody;
tempRedBrickOBj = Instantiate(redbrickObj , transform.position, transform.rotation);
tempRedBrick = redbrickObj.rigidbody;
}