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
3
Question by Anxo · Sep 23, 2010 at 11:10 PM · javascriptfunctionbooleandelay

Better way to delay a function for a few seconds? Javascript

It is more of an "inconvenience" than a problem.

I often find myself creating a bunch of booleans in order to stop something from happening more than ones.

Exsample

private var ReadyToFire : boolean = true;

function Update() {

if(pulltrigger && ReadyToFire) { ReadyToFire = false; fire(); resetReadyToFire(); } }

function resetReadyToFire(){ yield WaitForSeconds(2); ReadyToFire = true; }

Every time I do this, I feel like there has to be a simpler way to achieve what I am after. I always end up with a ton of functions and boolean variables just because I need to delay something.

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

1 Reply

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

Answer by Eric5h5 · Sep 23, 2010 at 11:36 PM

Don't use Update...it runs every frame, so to make stuff not happen every frame, you need a bunch of hacks, as you discovered. Just stick to coroutines instead.

function Start () {
   while (true) {
      while (!pullTrigger) yield;
      Fire();
      yield WaitForSeconds(2.0);
   }
}
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 Anxo · Sep 23, 2010 at 11:47 PM 0
Share

I will have to get a little bit more familiar with while statements. I am working on my 3rd iPhone game but I have been hacking my way thru every peace of code. I am a 3D Artist lol. I use "If" statements for everything.

if I use this while statment in the Start function, it will continue to be called all game even tho it is only called ones? I guess because it is a loop. Sounds good, I will look up how to use while statements, I think I saw a youtube video on it ones. Thanx for the tip, I am sure ones I get it down it can save me a lot of trouble.

avatar image Anxo · Sep 23, 2010 at 11:58 PM 0
Share

I just put it to the test, It works great. New weapon in my fight for creating games. Thank you.

avatar image Eric5h5 · Sep 24, 2010 at 12:06 AM 0
Share

@Anxo: A while loop continues to loop as long as the condition is true, and by just using "true", that means it will always loop, forever. (Since "true" is always true....) Although you can use "break" to stop it.

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

check bool in WaitForSeconds 2 Answers

auto run key 1 Answer

How to use functions between two scripts 2 Answers

determine the minimum time for the player to hold the button 1 Answer

Use yield WaitForSeconds to delay mouseover on a gameObject? (JS) 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