- Home /
How do you only Instantiate only once when pressing a button once?
Hi
Can anyone help me with this problem please? How do you only Instantiate a gameobject only once when pressing a button once? As the example below will instantiate a gameobject every time that a specific button is pressed. Thank you for your help.
var NPC_Guard : GameObject;
function Update () {
if(shoot.isDestroyed2 == true && Input.GetButtonDown("Fire1")){
bullsEyeTARGET2.renderer.enabled = false;
NPC_Guard = Instantiate(NPC_Guard, NPC_RespawnPoint1.transform.position, NPC_RespawnPoint1.transform.rotation);
NPC_Guard = Instantiate(NPC_Guard, NPC_RespawnPoint2.transform.position, NPC_RespawnPoint2.transform.rotation);
}
}
What do you mean??
You only want to instantiate ONE thing at a time rather than two things at once like your code does?
-OR-
You want to be able to push the button once, have it instantiate those two things up there at once, and then after that the button is a dead button and doesn't do anything anymore?
or what?
Yeah, it'd help if you only called Instantiate once, ins$$anonymous$$d of twice.
I meant the ability to push the button once, have it instantiate the two things once, and then after that the button is a dead button and doesn't do anything anymore.
Answer by mweldon · Jan 27, 2012 at 12:22 AM
How about add a local boolean variable, initialized to false, that is set to true when you press the button the first time and check the variable in your if statement.
But that will only ensure that everytime the fire button is pressed a new instantiated object will be created. It doesn't solve my problem I don't think. Using the above example script could you maybe implement what you said. $$anonymous$$aybe I've got the wrong end of the stick of what you are saying. It be better if you show me in code ins$$anonymous$$d of saying it. Cheers
var canClone : boolean = true ; function Update(){ if(whateverYouHaveHere && Input.GetButtonDown("Fire1") && canClone){ //all of that other stuff you have in here //otherstuff //otherstuff //when finished doing other stuff-> canClone = false ;
}
}
Answer by BiG · Jan 27, 2012 at 12:44 PM
@mweldon is totally right, so it deserves a +1. It's a very easy task, indeed; he have only pointed you to this:
var NPC_Guard : GameObject;
var lock = false;
function Update () {
if(shoot.isDestroyed2 == true && Input.GetButtonDown("Fire1") && lock == false){
lock = true;
bullsEyeTARGET2.renderer.enabled = false;
NPC_Guard = Instantiate(NPC_Guard, NPC_RespawnPoint1.transform.position, NPC_RespawnPoint1.transform.rotation);
NPC_Guard = Instantiate(NPC_Guard, NPC_RespawnPoint2.transform.position, NPC_RespawnPoint2.transform.rotation);
}
}
Just implemented that and it still makes clones of the original prefab when the button is pressed. I only want the clones to be generated no more the 2 times than that.
Whoops just realised some please ignore the previous comment.
Your answer
Follow this Question
Related Questions
How to get the key from the button name 1 Answer
multiple inputs 2 Answers
Creates object under itself, not parent 0 Answers
Trigger a function when player String input is same as name of object 1 Answer
[SOLVED] Only instantiating once 1 Answer