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 Saguinus · Mar 12, 2014 at 04:02 PM · coroutine

Coroutine does not seem to work

Hello,

I'm not very fond of coroutines yet, but here's basically what I'm trying to do: I want to change the color of a button (one out of four) for 1 second and I use states for that (Button_pressed = color, button_unpressed = no color). I change a bool variable in order to accomplish the state change.

Every round another button adds and I want to change the color of the buttons consecutively --> I need to delay the state changes of the buttons 2,3,4... otherwise the state changes will overlap.

I've tried many things so far and currently I'm stuck here:

Update() //Call GenerateNewButton() if the all the previous buttons have been displayed

GenerateNewButton() //Generate new Random (1-4) for the button that's supposed to change color and call PlayButton()

PlayButton() //Call the recursive function PlayCurrentButton

PlayCurrentButton() //Set Variable for the state change and call PlayCurrentButton again (with the next button) as long as there is a button to display

Here the exact code of the last function:

public void PlayCurrentButton(int Round)

 {

     if (PublicClass.ButtonList[Round] == 1){

         AnimatorDown.SetBool("DownPressed",true);

     }

     else if (PublicClass.ButtonList[Round] == 2){

         AnimatorRight.SetBool("RightPressed",true);

     }

     else if (PublicClass.ButtonList[Round] == 3){

         AnimatorUp.SetBool("UpPressed",true);

     }

     else if (PublicClass.ButtonList[Round] == 4){

         AnimatorLeft.SetBool("LeftPressed",true);

     }
     
     
     StartCoroutine(WaitForAnimation(Round));

     if (Round != PublicClass.ButtonList.Count-1) {
         PlayCurrentRound(Round+1);
     }
 }

 IEnumerator WaitForAnimation(int Round)
 {
     yield return new WaitForSeconds(10.0f);
 }

The problem is, that the Coroutine does not seem to work, there is no delay and the state changes overlap. I've already read many similar discussions about coroutines but I just can't seem to figure out the problem.

Thanks in advance ~

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
0

Answer by whydoidoit · Mar 12, 2014 at 04:09 PM

Your coroutine does nothing. Calling StartCoroutine from inside a normal function just starts that as a parallel track of execution and as it immediately waits and terminates.

If you wanted something to wait you would need to make the whole thing a coroutine or make a coroutine that does something after a period of time.

That latter can be achieved like this:

  public IEnumerator DoAfter(float delay, System.Action operation) {
        yield return new WaitForSeconds(delay);
        operation();
   }

And then you might call it in your code like this:

 public void PlayCurrentButton(int Round)
 
 {
  
     if (PublicClass.ButtonList[Round] == 1){
  
        AnimatorDown.SetBool("DownPressed",true);
  
     }
  
     else if (PublicClass.ButtonList[Round] == 2){
  
        AnimatorRight.SetBool("RightPressed",true);
  
     }
  
     else if (PublicClass.ButtonList[Round] == 3){
  
        AnimatorUp.SetBool("UpPressed",true);
  
     }
  
     else if (PublicClass.ButtonList[Round] == 4){
  
        AnimatorLeft.SetBool("LeftPressed",true);
  
     }
  
  
     StartCoroutine(DoAfter(10, ()=>{
       if (Round != PublicClass.ButtonList.Count-1) {
          PlayCurrentRound(Round+1);
       }
     }));
  
 }
Comment
Add comment · Show 2 · 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 Saguinus · Mar 12, 2014 at 04:32 PM 0
Share

Thanks for the help!

avatar image whydoidoit · Mar 12, 2014 at 04:58 PM 0
Share

Please tick the answer if it helps :D

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

21 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

Related Questions

Help needed with WaitForSeconds() 2 Answers

Coroutine... should I worry about synchronization or not? 1 Answer

Segment within a nested If does not get evaluated 2 Answers

Make a coroutine run when Time.timeScale = 0; 3 Answers

convert timer from update() to coroutine or invokerepeating 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