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 /
  • Help Room /
avatar image
0
Question by saahip · Feb 22, 2021 at 03:16 PM · scene-loadingscene-switchingdebuggingscene loaderror-handling

Problem with changing scenes: Stuck in a loop where two scenes keep loading each other

Hi guys! I'm fairly new to Unity, and I'm not sure how to fix this issue:

I have two scenes: TestTrial and TestTrialFailed (TTFailed).

After executing the TestTrial (which reads in 10 numbers and displays them onscreen), the scene TTFailed should (and does) appear. And when I press the button 'V' in the scene TTFailed, I should be able to (but am not) restart the TestTrial from the beginning (have all the 10 numbers appear again). I figured I'd implement that by just loading scene TestTrial again if V was pressed.

If I start on Scene TTFailed and press V, scene TestTrial loads and starts executing from scratch perfectly.

However, when I start from scene TestTrial, execute it, end up at scene TTFailed, and THEN press V, the scene TestTrial appears for a millisecond but it switches back to scene TTFailed!

I've set breakpoints in my scripts and found out that this is the issue:

(To load scenes I created a class called Loader that looks as follows:)

     public class Loader : MonoBehaviour
     {
        
         public enum Scene {
             TestTrial,
             TTFailed,
             //more scenes
         }
     
         public static void Load(Scene sc) {
            SceneManager.LoadScene(sc.ToString()); //fct from UnityEngine.SceneManagement that allows us to load a particular scene
         }
     }

In the Scene TestTrial (Script TestTrialGame) there are two static variables, bool reached and int count (gets incremented throughout the game). When all the 10 numbers have been displayed, reached is set to true. Scene TTFailed should be loaded once reached is true, so my code in the SceneChange script looks like this:

 void Update()
     {
         if (TestTrialGame.reached)
         {
             if (TestTrialGame.count >= 5) { //if this is true you want to load scene TTFailed
                 TestTrialGame.reached = false; //set reached to false bcos static variables don't get reset on scene load?
                 TestTrialGame.count = 0;
                 Loader.Load(Loader.Scene.TTFailed);
             }
             else {
                 //load another scene
             }
         }
     }
 



In the scene TTFailed, the code that performs the scene change when V is pressed looks as follows:

 void Update()
     {
      
         if (Input.GetKeyDown(KeyCode.V)) {
             Loader.Load(Loader.Scene.TestTrial);
         }     
     }
 

While setting breakpoints and debugging, I noticed that after performing Loader.Load(Loader.Scene.TTFailed); at the end of the TestTrial,

SceneManager.LoadScene(sc.ToString()); in the Loader Script is called with 'TTFailed' as argument, TTFailed scene is properly loaded and then when I press V, Loader.Load(Loader.Scene.TestTrial); in the TTFailed script is executed, which once again calls the Loader class code, BUT after getting out of Loader class, we end up back at the statement Loader.Load(Loader.Scene.TTFailed); instead of loading TestTrial completely and executing the scene from the beginning! And this even though 'reached' is set to false!

Can someone help me figure out how to solve this issue? Help would be very very much appreciated :)

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

0 Replies

· Add your reply
  • Sort: 

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

158 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

Related Questions

Start function not called after reloading scene 0 Answers

multiple of same objects from DontDesroyOnLoad 0 Answers

Multiple active scenes updating but not rendering? 0 Answers

[Unity Beginner] Game design question on scene loading and scene transitioning 0 Answers

changing scene not working? 0 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