• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by MusicKing7 · Oct 10, 2016 at 02:04 PM · c#unity 5error

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");
     }
 }
 
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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");
 }
Comment
MusicKing7

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MusicKing7 · Oct 10, 2016 at 02:32 PM 1
Share

Thanks so much! Great answer!

avatar image Bonfire-Boy · Oct 10, 2016 at 04:01 PM 0
Share

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.

avatar image

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;
             }
         }
    
    
    
    
    
    
Comment
Bonfire-Boy

People who like this

-1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Mar 26, 2019 at 05:45 PM 0
Share

So how does that relate to the question asked?

avatar image musicartslao Bonfire-Boy · Mar 27, 2019 at 08:55 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an object 0 Answers

Nav Mesh Problem with SetDestination 1 Answer

InvalidCastException: Cannot cast from source type to destination type. 1 Answer

error CS0201 1 Answer

How do I fix this code below!? | 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges