Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Random Binaries · Jan 26, 2012 at 09:56 PM · javascriptinstantiateinputbuttonclones

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

}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Jan 26, 2012 at 10:35 PM 2
Share

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?

avatar image syclamoth · Jan 26, 2012 at 11:31 PM 0
Share

Yeah, it'd help if you only called Instantiate once, ins$$anonymous$$d of twice.

avatar image Random Binaries · Jan 26, 2012 at 11:55 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Random Binaries · Jan 27, 2012 at 12:33 PM 0
Share

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

avatar image Lo0NuhtiK · Jan 27, 2012 at 12:41 PM 1
Share

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 ;

    }
 }

avatar image BiG · Jan 27, 2012 at 12:46 PM 1
Share

Yeah, Lo0Nuhti$$anonymous$$, you just ninja'd me! =D

avatar image Lo0NuhtiK · Jan 27, 2012 at 12:48 PM 1
Share

cue Bruce Lee sound effect-> "Wuuauaaaaaaa"

avatar image BiG · Jan 27, 2012 at 12:51 PM 1
Share

^_^ (6 characters to go)

avatar image
3

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);
 
 }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Random Binaries · Jan 27, 2012 at 12:55 PM 0
Share

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.

avatar image Random Binaries · Jan 27, 2012 at 12:57 PM 2
Share

Whoops just realised some please ignore the previous comment.

avatar image BiG · Jan 27, 2012 at 01:00 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges