The body of '' cannot be an iterator block because 'void' is not an iterator interface type???
Just now got this problem not knowing what to do. Trying to get a splash screen going by making the script wait for 5 seconds then switch scenes. Help?
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class splashScreen : MonoBehaviour {
// Update is called once per frame
void Update () {
yield return new WaitForSeconds (5.0f);
SceneManager.LoadScene("MainMenu1");
}
}
Answer by doublemax · Oct 10, 2016 at 02:26 PM
Yield can only be used inside a Coroutine.
void Update ()
{
StartCoroutine( NextScene() );
}
IEnumerator NextScene()
{
yield return new WaitForSeconds (5.0f);
SceneManager.LoadScene("MainMenu1");
}
A word of warning: this pattern could be dangerous in that it will result in the coroutine being started multiple times, because Update() will continue to be called every frame while the coroutine is waiting.
In this particular case I guess the first load scene call will result in the subsequent coroutines being killed, but one should be careful if using the pattern for other purposes, or if adding other code to the functions. You could use a flag to prevent the multiple coroutines.
Answer by musicartslao · Mar 26, 2019 at 04:01 PM
I am using here a Singleton pattern to create a GameManager and my solution is:
//010 fix the SceneManagement warning ! using UnityEngine.SceneManagement;
//01 replace T with this class (GameManager) public class GameManager : Singleton { //02 property that keeps track of the time remaining
and finally I use asyncload :
// Update is called once per frame void Update() { //05 subtracts the time since last frame from time remaining TimeRemaining -= Time.deltaTime; //06 checks if time remaining is 0 or negative if(TimeRemaining <= 0) { //07OLD reloads the current Level //Application.LoadLevel(Application.loadedLevel); OBSOLETE //011 calls the Coroutine for AsyncLoad StartCoroutine(LoadNextAsyncScene()); } } // fix the warning IEnumerator LoadNextAsyncScene() { //07 load the next scene async AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("02TriggerEventHealth"); //08 reset the timer for the next game back to maximum time TimeRemaining = maxTime; //09 wait for the async scene to full load while (!asyncLoad.isDone) { yield return null; } }
I was having the same error and fix it by calling the Coroutines OUTSIDE the Update function, as stated here:
// fix the warning
IEnumerator LoadNextAsyncScene()
One should also read this: https://docs.unity3d.com/ScriptReference/AsyncOperation.html