- Home /
Problem with coroutine.
Hello community.
I'm doing use of canvas UI Button for this.
On the inspector I call the "void StartGame" and it Works but the only thing is the Coroutine that it doesn't load 100% well.
The debug.log show me the stat "BeforWaiting2Seconds" But "AfterWaiting2Seconds" definitely won't show.
using UnityEngine;
using System.Collections;
public class InicioDelJuego : MonoBehaviour {
public AudioClip buttonStart;
public void MyButtonLoad ()
{
StartCoroutine (StartButton());
}
IEnumerator StartButton ()
{
Debug.Log ("BeforWaiting2Seconds");
yield return new WaitForSeconds (2);
Debug.Log ("AfterWaiting2Seconds");
}
public void StartGame()
{
MyButtonLoad ();
AudioSource.PlayClipAtPoint(buttonStart, new Vector3(0, 0, 0));
Application.LoadLevel("EnterLevel1Scene");
}
Any help will be appreciate, I'm sure is a foolishness but I can see what is it.
Regards.
I've tried to do a for with a break but it doesn't work it do the same.
using UnityEngine;
using System.Collections;
public class InicioDelJuego : $$anonymous$$onoBehaviour {
public AudioClip buttonStart;
private int seconds = 2;
private int secondsStart;
public void $$anonymous$$yButtonLoad ()
{
StartCoroutine (StartButton());
}
IEnumerator StartButton ()
{
Debug.Log ("BeforWaiting2Seconds");
yield return new WaitForSeconds (seconds);
Debug.Log ("AfterWaiting2Seconds");
{
for ( secondsStart = 0; secondsStart < seconds; secondsStart++ )
break;
}
}
public void StartGame()
{
$$anonymous$$yButtonLoad ();
AudioSource.PlayClipAtPoint(buttonStart, new Vector3(0, 0, 0));
Application.LoadLevel("EnterLevel1Scene");
}
Answer by Ryujose · Oct 10, 2014 at 06:39 PM
I'll put my code here for the one's having the same problema as me. With this it Works. Thanks for yout help Zentiu.
using UnityEngine;
using System.Collections;
public class InicioDelJuego : MonoBehaviour {
public AudioClip buttonStart;
private float seconds = 0.6f;
public void MyButtonLoad ()
{
StartCoroutine (StartButton());
}
IEnumerator StartButton ()
{
Debug.Log ("BeforWaiting0.6Seconds");
AudioSource.PlayClipAtPoint(buttonStart, new Vector3(0, 0, 0));
yield return new WaitForSeconds (seconds);
Debug.Log ("AfterWaiting0.6Seconds");
Application.LoadLevel("EnterLevel1Scene");
{
}
}
public void StartGame()
{
MyButtonLoad ();
}
Answer by HarshadK · Oct 10, 2014 at 06:48 PM
I guess I'm late for the party since question is already answered but I'll post the reason behind the behavior.
You are calling MyButtonLoad from your StartGame. And MyButtonLoad calls coroutine and everything before yield executes from it. So after the MyButtonLoad is called it moves to next line in StartGame and loads a scene where your coroutine stops hence nothing after yield executes. If you comment Application.LoadLevel statement you can see coroutine will execute fully.