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 maggot · Aug 22, 2010 at 01:32 PM · fireoncecharge

Calling a function only once whenever conditions are correct

I have code which builds up charge while a button is held down and then I want it to execute a function only once. This is within a function called ScanInput() which is called from Update()

while(Input.GetButton("Fire1"))// && (Time.deltaTime > nextFire)) { //Add to the charge as long as the // user is holding the button chargeLevel += Time.deltaTime * chargeSpeed; firedMissile = false; firePlayer001Missile(firedMissile); yield; }

 chargeLevel = 0.0;

Heres the code for the function firePlayer001Missile

function firePlayer001Missile(firedMissile)  
{
if(!firedMissile)
{
    var player001Missile = new Instantiate(currentMissileType, GameObject.Find("Player001ShotSpawnPoint001").transform.position,Quaternion.identity);
currentMissileType.transform.Translate(0,shootForce*Time.deltaTime,0);
player001Missile.rigidbody.AddForce(transform.up*shootForce);
bulletCount++;
firedMissile=true;
}
return firedMissile;
}

As it is now, it fires a barrage of bullets which I think is equal to the number of calls to the function firePlayer001Missile; I want it to fire only one bullet after the fire button has been held down.

Comment
Add comment · Show 4
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 matyicsapo · Aug 22, 2010 at 02:14 PM 1
Share

check GetButtonDown and GetButtonUp, also I prolly wouldn't use a coroutine for this

avatar image maggot · Aug 22, 2010 at 05:20 PM 0
Share

Thanks, i got it working

avatar image Peter G · Aug 22, 2010 at 08:59 PM 2
Share

You changed it from the code I gave you in a previous question. the fired$$anonymous$$issle should go after the while loop if you haven't changed the design, which it looks like you haven't. @$$anonymous$$aggot, There is nothing wrong with the coroutine. It would work the same as having an if statement and a few other bools. See the original answer http://answers.unity3d.com/questions/18013/power-up-weapon-shot-before-releasing-and-resetting-power/18015#18015

avatar image maggot · Aug 23, 2010 at 05:02 PM 0
Share

I tried a few different options

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by maggot · Nov 16, 2011 at 01:05 PM

I got this working by having a bool set to false. Then an If condition for false on the bool (and it always is false the first time the code is run), followed by the conditional code I want to run once and ending by setting the bool to true.

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Different colored jet streaks from fire particle system 2 Answers

Trouble with script to fire a bullet from a handgun 1 Answer

C# RayCast Fire Cooldown DeltaTime Help 1 Answer

How to make extinguish? 1 Answer

Bullets won't fire? 0 Answers


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