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
1
Question by RealMTG · Nov 20, 2014 at 11:07 PM · startcoroutineonce

Run Coroutine only once

Hi

I am making a small minigame and I want it so when I reach 50 points it will start to spawn the fast enemies. How would I do something like this? If I did it like this...

 void Update(){
     if(score >= 50){
         StartCoroutine(SpawnFastEnemies());
     }
 }

... it would start the same coroutine several times. How do I do something like this?

Thanks

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
7
Best Answer

Answer by shadbags · Nov 20, 2014 at 11:12 PM

Couple of ways. If the coroutine is necessary, I'd create a flag (a simple boolean variable) in this file, called something like

 bool fastWavesStarted = false;

Then, alter your code to use this flag, and once it's fired, the flag will stop the coroutine running many many times.

 void Update(){
     if(score >= 50 && fastWavesStarted == false){
         StartCoroutine(SpawnFastEnemies());
         fastWavesStarted = true;
     }
 }

This way, you start the coroutine, then immediately make it so it can't start again. If you DO want to restart it, you can just set the flag back to false.

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 RealMTG · Nov 20, 2014 at 11:25 PM 0
Share

Thanks! You really saved me!

avatar image MattLebrao · Mar 07, 2017 at 10:37 PM 1
Share

I'm having a very weird problem with this approach... $$anonymous$$y flag is somehow being reset and the StartCoroutine method is being called exactly twice (for aparent reason). I'm trying to prevent a new instance of the coroutine while the previous one is still running. Here is my code:

 void Update()
 {
     if (!IsRunning)
     {
         IsRunning = true;
         StartCoroutine("$$anonymous$$oveToTarget");
     }
 }

I have used breakpoints to check exactly how the coroutine is flowing and in fact the line that sets the IsRunning flag back to false is only running when the object has reached the destination and the routine ends (allowing it to be set in montion towards the next target). However, again using breakpoints, I found out that before the first instance of the coroutine calls IsRunning = false, the Update method is called again and for some demonic reason the flag is reset to false! So it starts a second instance of the coroutine... And the weirdest, it only calls exactly two instances. What could it possibly be??

This is embarassing...

t turns out I didn't realize I had two instances of my script attached to the game object, so the second time the breakpoint was hit and the flag was "magically" reset it was actually a different instance of the entire $$anonymous$$onoBehaviour... I will soon delete my comment in shame and go cry at the corner...

avatar image LoboBobo123 · Jul 13, 2020 at 10:17 PM 0
Share

I had the same problem with my project. Been trying to find a solution for the last 2 days. Yours worked like a charm! You have my eternal gratitude!!!

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

29 People are following this question.

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

Related Questions

I'd like to move a cube without delay.(C#) 0 Answers

StartCorutine not Working 3 Answers

How to force Coroutine to finish 1 Answer

Is there a way to show a warning when a Coroutine is called directly? 0 Answers

Rotation: rotate by a defined angle 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