Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WaitForSeconds(mySource.clip.length); doesn't work

Discussion in 'Scripting' started by Smiaro, Jan 7, 2016.

  1. Smiaro

    Smiaro

    Joined:
    Jul 22, 2013
    Posts:
    19
    Hello, I have something like:
    Code (csharp):
    1. IEnumerator PlayFirst() {
    2. mySource.Play();
    3. yield return new WaitForSeconds(mySource.clip.length);
    4. }
    called with:
    Code (csharp):
    1.  StartCoroutine("PlayFirst");
    where I want music to play.
    But application doesn't wait for clip to stop playing.
    How to fix that?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    you don't do anything after yielding the WaitForSeconds... what's supposed to be waiting?
     
  3. Svarr

    Svarr

    Joined:
    Dec 20, 2015
    Posts:
    22
    You can add a simple Debug.Log line to the PlayFirst Method to check if it's called at all.
    Do you call it from the same script?
     
  4. Smiaro

    Smiaro

    Joined:
    Jul 22, 2013
    Posts:
    19
    Code (csharp):
    1. void Update() {
    2. if(gestureListener.IsWave()) {
    3. if(whichFlower==1){
    4. //something here
    5. StartCoroutine("PlayFirst");
    6. //WANT TO WAIT HERE before next lines
    7. //something here
    8. }
    9. }
    @lordofduct
    I have fixed that with huge ammount of additional code. I am just wondering if there is possibility to use just WaitForSecondsEtc to stop on that line until audio is playing.
     
  5. Svarr

    Svarr

    Joined:
    Dec 20, 2015
    Posts:
    22
    I think the wait is only applied to the co routine, not the method that calls it. You would have to put the lines whose execution you want to delay into the co routine.
     
    Magiichan likes this.
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    yeah, the wait only makes the coroutine wait... calling StartCoroutine doesn't make Update wait.
     
  7. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    730
    Alternatively you could nest your coroutine in another one.

    Code (CSharp):
    1. //Inside first coroutine, this will wait for PlayFirst to finish before continuing
    2. yield return StartCoroutine("PlayFirst");
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    As mentioned, use yield return for the actual coroutine. Instead of using the lenght of the clip, this could is more reliable.

    Warning: UNTESTED CODE
    Code (csharp):
    1. IEnumerator PlayFirst() {
    2.   mySource.Play();
    3.   while (mySource.isPlaying) {
    4.     yield return (null);
    5.   }
    6. }
     
    durukanozanalp likes this.