- Home /
Changing scenes with unity 4.6 UI
So I'm trying to have a fade effect and I need it to wait a certain time, but I can't get it to run an IEnumerator and a void won't wait. Does anyone know how to overcome this?
using UnityEngine;
using System.Collections;
public class UINextLevel : MonoBehaviour {
public void NextLevelButton(int index)
{
float fadeTime = GameObject.Find ("GM").GetComponent<FadingLevel> ().BeginFade (1);
yield return new WaitForSeconds (fadeTime);
Application.LoadLevel(index);
}
IEnumerator change(string levelName){
}
}
Comment
Best Answer
Answer by g8minhquan · Nov 27, 2014 at 06:25 AM
Your code is wrong, you yield return in a normal function, while doesn't yield return anything in the Coroutine. It should be something like this:
public void NextLevelButton(int index)
{
StartCoroutine(Change());
}
IEnumerator Change(){
yield return new WaitForSeconds (fadeTime);
Application.LoadLevel(levelName);
}
That's the answer. You can get around every one of the restrictions on function calls from the UI buttons by using wrapper methods that do meet the criteria.