x


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);

}

}

more ▼

asked Jan 26 '12 at 09:56 PM

Random Binaries gravatar image

Random Binaries
29 6 10 12

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?

Jan 26 '12 at 10:35 PM Lo0NuhtiK

Yeah, it'd help if you only called Instantiate once, instead of twice.

Jan 26 '12 at 11:31 PM syclamoth

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.

Jan 26 '12 at 11:55 PM Random Binaries
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

@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);

}
}
more ▼

answered Jan 27 '12 at 12:44 PM

BiG gravatar image

BiG
4.7k 4 13 49

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.

Jan 27 '12 at 12:55 PM Random Binaries

Whoops just realised some please ignore the previous comment.

Jan 27 '12 at 12:57 PM Random Binaries

That's good. Let us know if there are other troubles.

Jan 27 '12 at 01:00 PM BiG
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 27 '12 at 12:22 AM

mweldon gravatar image

mweldon
252 1 5

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. Maybe I've got the wrong end of the stick of what you are saying. It be better if you show me in code instead of saying it. Cheers

Jan 27 '12 at 12:33 PM Random Binaries

    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 ;
          
       }
    }
Jan 27 '12 at 12:41 PM Lo0NuhtiK

Yeah, Lo0NuhtiK, you just ninja'd me! =D

Jan 27 '12 at 12:46 PM BiG

cue Bruce Lee sound effect-> "Wuuauaaaaaaa"

Jan 27 '12 at 12:48 PM Lo0NuhtiK

^_^ (6 characters to go)

Jan 27 '12 at 12:51 PM BiG
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3445
x1670
x952
x786
x25

asked: Jan 26 '12 at 09:56 PM

Seen: 1238 times

Last Updated: Jan 27 '12 at 01:00 PM