Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Soareverix · Sep 07, 2020 at 08:18 AM · script.scene-loadingwaitforsecondsienumerator

SceneManager.LoadScene not working ONLY when it is in IEnumerator functions

I've experienced a weird error: when I call SceneManager.LoadScene in any (emphasis on any) IEnumerator function, it doesn't work. Here's an example of what isn't working:

 IEnumerator TagDisplay()
     {
         yield return new WaitForSeconds(updateSpeed);
         float tagsActive = GameObject.FindGameObjectsWithTag(tagToSearchFor).Length;
         
         if (tagsActive < 1)
         {
             tagText.text = ifNoTagsLeft;
             yield return new WaitForSeconds(5);
             Debug.Log("hi!");
             if (setHighScore)
             {
                 setHighScore = false;
                 highscoreObject.timerStop = true;
                 Debug.Log("Score set!");
             }
             //LoadLevel();
             if (levelToMoveToElseBlank != "")
             {
                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //go to next level once enemies are dead
             }
             else
             {
 
                 SceneManager.LoadScene(levelToMoveToElseBlank); //go to next level once enemies are dead
                 Debug.Log("Level loaded!");
             }
 
         }
         else
         {
             tagText.text = introString + tagsActive.ToString();
         }
         StartCoroutine(TagDisplay());
     }

Another example of code that doesn't work:

 IEnumerator TagDisplay()
     {
         yield return new WaitForSeconds(updateSpeed);
         float tagsActive = GameObject.FindGameObjectsWithTag(tagToSearchFor).Length;
         
         if (tagsActive < 1)
         {
             tagText.text = ifNoTagsLeft;
             yield return new WaitForSeconds(5);
             Debug.Log("hi!");
             if (setHighScore)
             {
                 setHighScore = false;
                 highscoreObject.timerStop = true;
                 Debug.Log("Score set!");
             }
             LoadLevel();
             
 
         }
         else
         {
             tagText.text = introString + tagsActive.ToString();
         }
         StartCoroutine(TagDisplay());
     }
 
     void LoadLevel()
     {
         if (levelToMoveToElseBlank != "")
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //go to next level once enemies are dead
         }
         else
         {
             
             SceneManager.LoadScene(levelToMoveToElseBlank); //go to next level once enemies are dead
             Debug.Log("Level loaded!");
         }
         /*
         Debug.Log("Level loaded!");
         SceneManager.LoadScene(levelToMoveToElseBlank);
         */
     }

Strangest of all... this used to work. This code worked in my game and all my other scenes for nearly six weeks. Somehow, I seem to have screwed up every IEnumerator in my project sometime in the last week or two. If I use Debug.Log("hi") in an IEnumerator, it still prints out after a few seconds, but it never runs the scene change after a delay.

Some code I ended up writing that does work (it loads the next level instantly, which is a little jarring):

 void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             
             //FractureManager.collisionNum++;
             Destroy(gameObject);
 
             //yield return new WaitForSeconds(0.01f);
             /*
             brokenPiece1.transform.localScale = originalPiece.transform.lossyScale * 49f;
             brokenPiece2.transform.localScale = originalPiece.transform.lossyScale * 49f;
             brokenPiece3.transform.localScale = originalPiece.transform.lossyScale * 49f;
             brokenPiece4.transform.localScale = originalPiece.transform.lossyScale * 49f;
             brokenPiece5.transform.localScale = originalPiece.transform.lossyScale * 49f;
             brokenPiece6.transform.localScale = originalPiece.transform.lossyScale * 49f;
             brokenPiece7.transform.localScale = originalPiece.transform.lossyScale * 49f;
             */
 
             //clone = brokenPiece.transform.localScale = originalPiece.transform.lossyScale;
             if (!shattered)
             {
                 shattered = true;
                 GameObject clone;
                 clone = Instantiate(brokenPiece, originalPiece.position, originalPiece.rotation);
                 clone.transform.GetChild(0).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
                 clone.transform.GetChild(1).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
                 clone.transform.GetChild(2).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
                 clone.transform.GetChild(3).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
                 clone.transform.GetChild(4).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
                 clone.transform.GetChild(5).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
                 clone.transform.GetChild(6).gameObject.transform.localScale = originalPiece.transform.lossyScale * 49;
             }
             Debug.Log("start new elvel");
             
             highscoreTimer.timerStop = true;
             //StartCoroutine(LoadNewLevel());
             LoadNewLevel();
 
 
         }
         
     }
 
     void LoadNewLevel()
     {
         //yield return new WaitForSeconds(waitAfterHit);
         SceneManager.LoadScene(levelName);
     }


Any thoughts?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Namey5 · Sep 07, 2020 at 09:07 AM

SceneManager was kinda built to be run asynchronously - as such, LoadLevel does some weird things with timing and it is recommended you use LoadLevelAsync in most cases. Because you are already calling this from a coroutine, it's even more appropriate;

 ...
     Debug.Log("Score set!");
 }
 //LoadLevel();
 if (levelToMoveToElseBlank != "")
 {
     yield return SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1); //go to next level once enemies are dead
 }
 else
 {
 ...

https://docs.unity3d.com/ScriptReference/AsyncOperation.html

Comment
Add comment · Show 1 · 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 Soareverix · Sep 08, 2020 at 03:04 AM 0
Share

Really appreciate it! This works! For a little while, I though it wasn't working still, and then I realized that I'd misspelled the level that I wanted to move to. So, that could be a problem for someone else, but in this case, I was legitimately having a problem with the loadScene separate from misspelling. Your solution worked!

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.

Follow this Question

Answers Answers and Comments

176 People are following this question.

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

Related Questions

same script for two scenes, how can i call a function for particular scene with out affecting that function for another scene?? 0 Answers

How to make a 3d text writeOut method? 1 Answer

IEnumerator problem 1 Answer

Run Coroutine once, but finish Lerping 2 Answers

Pause in C# 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