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 UnbreakableOne · Jan 24, 2014 at 04:27 PM · coroutine

How to start a coroutine from Update.

Hi,

I check something in my Update function and depending on some parameters I start a coroutine that does something in 5 seconds.

Since Update is called constantly, it's perfect to check for my condition but when it gets true, it will stay true and call the coroutine several times and I don't think it's good.

I can hackaway with a boolean but I was wondering if my approach is wrong since I'm new to Unity and coroutines. Here is my code:

     void Update ()
     {
         if (m_objectiveManager.IsObjectivesDone)
         {
             StartCoroutine("RestartGame");
         }
     }
 
     IEnumerator RestartGame()
     {
         Debug.Log ("We believe that game is over and will restart the game in 5 seconds.");
         yield return new WaitForSeconds(5.0f);
         Application.LoadLevel (Application.loadedLevel);
     }
Comment
Add comment · Show 1
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 IndieScapeGames · Jan 24, 2014 at 04:49 PM 0
Share

I always use a boolean to flip once a criteria has been met.

4 Replies

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

Answer by Bunny83 · Jan 25, 2014 at 03:24 AM

There's a way simpler solution: Don't use Update at all ;)

 void Start()
 {
     StartCoroutine(RestartGame());
 }
  
 IEnumerator RestartGame()
 {
     while (!m_objectiveManager.IsObjectivesDone)
         yield return null;
     Debug.Log ("We believe that game is over and will restart the game in 5 seconds.");
     yield return new WaitForSeconds(5.0f);
     Application.LoadLevel (Application.loadedLevel);
 }
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
avatar image
1

Answer by ankush_Kushwaha · Jan 24, 2014 at 04:52 PM

you have two options

1) This is your approach with some improvement:

 void Update ()
     {
        if (m_objectiveManager.IsObjectivesDone)
        {
           m_objectiveManager.IsObjectivesDone = false;
          StartCoroutine("RestartGame");
        }
     }
  
     IEnumerator RestartGame()
     {
        Debug.Log ("We believe that game is over and will restart the game in 5 seconds.");
        yield return new WaitForSeconds(5.0f);
        Application.LoadLevel (Application.loadedLevel);
     }




2) I will suggest you to follow second one that is

 void Update(){
     
           if(Time.time - _lastTime > 5){
 
              _lastTime = Time.time;
               //  check your stament here and do the stuff
        }
 
 
 }
Comment
Add comment · Show 1 · 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 oatsbarley · Jan 24, 2014 at 04:58 PM 1
Share

Without knowing what other parts of the game utilise m_objective$$anonymous$$anager.IsObjectivesDone, it could potentially be very disruptive to modify it. Heck, chances are it's a read-only property.

avatar image
0

Answer by oatsbarley · Jan 24, 2014 at 04:49 PM

Setting a flag the first time the coroutine is called isn't such a bad thing.

The alternative is having a state enum variable and switching it depending on the state of the game. So for instance it could be GameState.Playing normally, then set to GameState.Restarting when the coroutine gets run. State machines are common in games programming, just because games are very stateful systems usually.

If you're not going to use states then a single flag set when the coroutine gets called and checked against the next time round isn't a bad idea at all.

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
avatar image
0

Answer by gfallasc · Jan 24, 2014 at 04:59 PM

Hi, I think calling a Coroutine from Update is not the appropriate, but in case you want so:

 void Update ()
      {
            if (m_objectiveManager.IsObjectivesDone)
            {
                //Just called once
                m_objectiveManager.IsObjectivesDone = false; //If the property/var can be set!
                StartCoroutine("RestartGame");
            }
     }
     
     //But if you're not using State Machines, you should call the Coroutine out of the Update
     //May be when you set the m_objectiveManager.IsObjectivesDone var like this
     
     //Not in the Update
     m_objectiveManager.IsObjectivesDone = true;
     //Something else
     //Then call the Coroutine
     StartCoroutine("RestartGame");

Hope it helps

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

23 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

Related Questions

Fade object in smoothly not working 1 Answer

Multi-threaded / non-blocking way of encoding images to Texture2D 1 Answer

StopCoroutine() is not stopping my coroutines 1 Answer

Hold or Wait while Coroutine finishes 6 Answers

lerp in coroutine doesn't work correctly 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