Search Unity

IEnumerator not working with GUI.Button

Discussion in 'Scripting' started by Longurimont, Feb 27, 2017.

  1. Longurimont

    Longurimont

    Joined:
    May 18, 2014
    Posts:
    8
    For some reason this doesn't work:

    Code (CSharp):
    1.     IEnumerator PlaySound() {
    2.         ClickSource.Play();
    3.         yield return new WaitForSeconds(ClickSource.clip.length);
    4.     }
    Code (CSharp):
    1.        
    2. if (GUI.Button(new Rect(Screen.width - (Screen.width / 4), Screen.height - (Screen.height / 4), Screen.width / 10, Screen.height / 12), "Play"))
    3.         PlaySound();
    4.        
    But this does:

    Code (CSharp):
    1.        
    2. if (GUI.Button(new Rect(Screen.width - (Screen.width / 4), Screen.height - (Screen.height / 4), Screen.width / 10, Screen.height / 12), "Play"))
    3.         ClickSource.Play();
    Can someone help me? Thank you so much for your time :)
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You cant just call PlaySound, you must StartCoroutine( Playsound() )
     
    Longurimont likes this.
  3. Longurimont

    Longurimont

    Joined:
    May 18, 2014
    Posts:
    8
    Thank you!
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You're also literally waiting for nothing with this code.
     
  5. Longurimont

    Longurimont

    Joined:
    May 18, 2014
    Posts:
    8
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, if there's no code that you want to execute after you've waited, then there's no need to wait at all.
     
  7. Longurimont

    Longurimont

    Joined:
    May 18, 2014
    Posts:
    8
    oh ok, thank you :)