Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RetroGeek46 · Oct 18, 2017 at 06:14 PM · coroutinewaitforsecondsdelayyeild

Coroutine not working as expected

I want to change state of a Gameobject using script. While going from one state to another, I want a little delay to be introduced.

The script as of now is

 public enum TextState {
     Start,
     ChooseAction
 }

 public TextState currentState;

 void Start () {
     currentState = TextState.Start;
 }
 void Update () {
         if (currentState == TextState.Start)
             doStart();
         if (currentState == TextState.ChooseAction)
             doChooseAction();
     }
 }

 void doStart(){
     var t = GameObject.Find ("TextBox").GetComponent<TextAnimation>()    ;
     t.showText ("X attacked you!!");
     StartCoroutine(doWait());
     currentState = TextState.ChooseAction;
 }

 void doChooseAction(){
     Debug.Log("On ChooseAction");
 }
     
 IEnumerator doWait(){
     yield return new WaitForSecondsRealtime(100);
     Debug.Log ("waited for 100");
 }

showNext is a function defined in another script used to animate text using coroutine, with letters appearing after short delay.

As of now the Debug statement in doWait() isn't being run, and state immediately changes to chooseAction

What I know till now is that yield in my coroutine gives control back to rest of code and the subsequent lines aren't running. If anyone could help me with this and point out how to implement the coroutine it'd be a great help.

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

Answer by MacDx · Oct 18, 2017 at 06:33 PM

You just need to move this line:

 currentState = TextState.ChoosenAction;

And put it inside the coroutine.

So it would look like this:

 IEnumerator doWait()
 {
     yield return new WaitForSecondsRealtime(100);
     currentState = TextState.ChooseAction;
     Debug.Log("waited for 100");
 }

Corotuines will yield but only for stuff that is inside them, not outside. Also try reducing the wait time to like 3 seconds, just for testing. Waiting more than a minute every time you want to test the code, seems like a waste of time :P

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 RetroGeek46 · Oct 18, 2017 at 07:01 PM 0
Share

Working correctly now. The time was big because the change was instantaneous so just put a big number. $$anonymous$$any thanks

avatar image Bunny83 · Oct 18, 2017 at 07:44 PM 0
Share

$$anonymous$$oving this line:

 currentState = TextState.ChoosenAction;

after the yield is not a good idea as Update will continue to call "doStart" every frame and start a new coroutine every frame.

avatar image RetroGeek46 Bunny83 · Oct 18, 2017 at 07:49 PM 0
Share

I did think of it and have added a bool which is true whenever a doSomething() is entered and false as soon as doWait() is finished and if statment in update checks if the bool is false. This way update only changes states when no other state is running. I hope this is a correct approach.

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

75 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 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 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

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

How to create a delay in a function being called in update? 2 Answers

Coroutine not running after yield return new WaitForSeconds 3 Answers

How can i get this Delay script to work? 1 Answer

Can IEnumerator be called more than 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