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 andycodes · May 25, 2017 at 04:24 PM · coroutinewaitforsecondsienumerator

yield return waitforseconds not waiting?

I have a script which fires a coroutine on start which activates necessary items in my level. This also calls an animation IEnumerator to phase the ui animation in and out. However, there is a lag when unity is playing an animation right away, so I wanted to write in a wait for seconds to let the editor initialize then run my animation coroutine. the script looks something like this:

 start(){
 startCoroutine(beginCourse());
 }
 
 IEnumerator beginCourse(){
 yield return new waitForSeconds(2); //waits
 
 yield return startCoroutine(levelBeginAnimation());  /fires opening animations
 
 }

My problem is that the script seems to start the waitfor seconds, then starts the level begin animation coroutine, but the thing is that the wait for seconds doesnt yield. I've tried this in my code with print statements as well to be certain. Is there something I'm doing wrong here?

EDIT: actual code posting that I'm using with only pertaining code :

CoursePrefs.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CoursePrefs : MonoBehaviour {
 
     //public string courseName;
 
     //public int currentHole = 0;    //0 based hole index
 
    // public HolePrefs[] courseHoles;
    
     //public zoomTouch zoomFocus;
     //public CameraPin pin;
     //public GameObject testgui;
 
     public UIManager _UIManager;
 
     private void Start()
     {
         //makes sure zoom is at max first.
         //Camera.main.orthographicSize = zoomFocus.maxZoom;
         
         StartCoroutine(beginCourse());
     }

     IEnumerator beginCourse() {
     yield return new WaitForSeconds(2);
     
     yield return StartCoroutine(_UIManager.transitionAnimation());
     //wait for these to finish then do this stuff;
     //getCurrentHole().thisStartGreen.GetComponent<PlaceGolfBall>().enabled = true;
     //pin.golfball = getCurrentHole().thisStartGreen;
     //pin.isFollowing = true;
     //zoomFocus.canZoom = true;
     
 }
 
 }


UIManager.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class UIManager : MonoBehaviour {
 
     //the transitional UI that plays between holes to notify which hole is currently being played
     public GameObject transition_holeNum;
     public GameObject transition_frontNine;
     public GameObject transition_backNine;
 
     //Menus that play when the hole is made
     public GameObject menuEndHole;
 
     //Pause Menu
     public GameObject menuPauseScreen;
 
     //Controls the transitional UI. Hangtime is how long the menu stays between states before conitnuing the swipe out.
     public IEnumerator transitionAnimation(float hangTime = 2) {
         yield return StartCoroutine(transitionSwipeIn());
         yield return new WaitForSeconds(hangTime);
         yield return StartCoroutine(transitionSwipeOut());
     }
 
 //Controls the swipe in. offsetTime is the time between when the last animation began and the next one starts of the 3 panes.
     public IEnumerator transitionSwipeIn(float offsetTime = 0.25f) {
         if (transition_holeNum != null) {
             transition_holeNum.GetComponent<UIController>().Show();
         }
         
         if (transition_frontNine != null) {
             yield return new WaitForSeconds(offsetTime);
             //start swipe in
         }
         
         if (transition_backNine != null) {
             yield return new WaitForSeconds(offsetTime);
             //start swipe in
         }
         //think about yielding a wait for seconds for how long it takes the bar to reach its destination so the animation fully completes before exiting the coroutine.   
     }
 //Controls the swipe out
     public IEnumerator transitionSwipeOut(float offsetTime = 1)
     {
         if (transition_holeNum != null)
         {
             transition_holeNum.GetComponent<UIController>().Hide();
         }
        
         if (transition_frontNine != null)
         {
             yield return new WaitForSeconds(offsetTime);
             //start swipe out
         }
         
         if (transition_backNine != null)
         { 
             yield return new WaitForSeconds(offsetTime);
             //start swipe out
         }
     }
 }









Comment
Add comment · Show 3
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 RobAnthem · May 23, 2017 at 06:11 PM 0
Share

This... doesn't make sense. I already dislike coroutines, but this makes even less since than using a coroutine in the first place. If all you want is to start the level in 2 seconds you can just do something like

 void Start()
 {
     Invoke("BeginLevel", 2.0f);
 }
 void BeginLevel()
 {
     //Begin Level code
 }
avatar image tanoshimi · May 23, 2017 at 06:14 PM 0
Share

Please post the actual code you're using - what you've written wouldn't even compile.

But @RobAnthem is correct - it's unclear why you aren't just using Invoke() in this case.

avatar image andycodes tanoshimi · May 23, 2017 at 06:28 PM 0
Share

I've updated the code with pertaining code from the actual scripts

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jayaluru · Jun 13, 2017 at 02:25 AM

I have a similar situation, where the call of 'StartCoroutine (waitseconds()); ' works fine but the unity does not wait for the specified time.

      IEnumerator waitseconds()
     {
         yield return new WaitForSecondsRealtime(4);
         Debug.Log("hey i waited 4 secs of my life");
     }
 
     // Update is called once per frame
     void Update () {
 
         Debug.Log("hello can u wait");
         StartCoroutine(waitseconds());
         Debug.Log("its me i waited");
      }

The comments work fine suggesting that the call was made successfully but unity does not wait. Can anyone of you help me out??

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 corriedotdev · Aug 06, 2018 at 06:08 PM 0
Share

it wont wait because its in an update, take that start couroutine out the update and put it in void start and it will wait. or throw it where you want to call it from

avatar image
0

Answer by ankitdave · Jun 13, 2017 at 07:15 AM

Hi @jayaluru may be it works for you

public bool waitbool=false;

 IEnumerator waitseconds()
 {
     yield return new WaitForSecondsRealtime(4);
     Debug.Log("hey i waited 4 secs of my life");
     waitbool = true;
 }

 // Update is called once per frame
 void Update()
 {

     Debug.Log("hello can u wait");
     StartCoroutine(waitseconds());
     if (waitbool == true)
     {
         Debug.Log("its me i waited");
     }
 }
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 corriedotdev · Aug 06, 2018 at 06:15 PM

They are both being executed concurrently. You need to finish one coroutine then call the other, not line by line. Let the wait for seconds yield then start your coroutine.

     void Start(){
     StartCoroutine(beginCourse());
 }    
 
     IEnumerator beginCourse() {
         Debug.Log("waiting..");
         yield return new WaitForSeconds(2); //waits
 
         StartCoroutine(animateblah());  // fires opening animations
 
 
      }
 
     private IEnumerator animateblah() {
         Debug.Log("Animating");
         yield return null;
     }

This will work.

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 Bunny83 · Aug 06, 2018 at 08:11 PM 0
Share

You are doing exactly the same as the OP. He also waits for the nested coroutine to finish which is pretty pointless since nothing else is happening inside the coroutine after the nested one. However this doesn't change anything. His original code does work just fine. He most likely has a different problem. Either the initial lag is already 2 seconds or something else is going on he didn't show us.

avatar image corriedotdev Bunny83 · Aug 06, 2018 at 09:41 PM 0
Share

I tested this and it works. Youre not yielding a cooroutine that doesnt require it in the begin

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

72 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

Related Questions

yield return new WaitForSeconds () not working 1 Answer

Need Help with a Null Reference 1 Answer

Can't get past WaitForSeconds in my coroutine 1 Answer

Coroutine doesn't work when called from another method. 3 Answers

How to stop coroutines, when paused 2 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