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 romeo160705 · Jul 05, 2020 at 03:59 PM · scripting problemsceneloadscene-switchingscene load

Everytime when i start at level 3 or higher after completing it it goes back to level 2 how do i fix this?

I made a game from a tutorial on youtube and i have an level selection scene after you press play but know when i go to level 3 or higher from that level selection scene and i complete that level i am goin back to level 2 but when i start at level 1 or 2 i just go to the right scenes.

All my levels are called Level1 Level2 Level3 Level4 etc.

so this code should work.

 public class LevelController : MonoBehaviour
 {
     private static int _nextLevelIndex = 1;
     private Enemy[] _enemies;
 
     private void OnEnable()
     {
         _enemies = FindObjectsOfType<Enemy>();
     }
 
     void Update()
     {
        foreach(Enemy enemy in _enemies)
         {
             if (enemy != null)
                 return;
         }
 
         Debug.Log("You killed all enemies");
 
         _nextLevelIndex++;
         string nextLevelName = "Level" + _nextLevelIndex;
         SceneManager.LoadScene(nextLevelName);
     }
 }

Here is the code for my level selection scene i dont think the problem is on this code

 public class lvlControl : MonoBehaviour
 {
    public void selectScene()
     {
         switch (this.gameObject.name)
         {
             case "Level1":
                 SceneManager.LoadScene("Level1");
                 break;
 
             case "Level2":
                 SceneManager.LoadScene("Level2");
                 break;
 
             case "Level3":
                 SceneManager.LoadScene("Level3");
                 break;
 
             case "Level4":
                 SceneManager.LoadScene("Level4");
                 break;
 
 
             case "Level5":
                 SceneManager.LoadScene("Level5");
                 break;
 
 
             case "Level6":
                 SceneManager.LoadScene("Level6");
                 break;
 
 
             case "Level7":
                 SceneManager.LoadScene("Level7");
                 break;
 
 
             case "Level8":
                 SceneManager.LoadScene("Level8");
                 break;
         }
     }
        
     
 }

it all worked fine until i went from 4 to 8 levels and know it doesnt work anymore.

I hope someone knows how to fix this and i would really appreciate if you reply to this.

Comment
Add comment · Show 1
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 xxmariofer · Jul 05, 2020 at 04:41 PM 0
Share

_nextLevelIndex value at the start is always 1 and will always be 2 when you call the LoadScene method?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mbro514 · Jul 05, 2020 at 07:58 PM

This should solve your problem. Unity Answers Page

I also have a suggestion that is completely unrelated to your problem. Instead of using the switch statement in your second script, you could just write:

 public void selectScene()
 {
 SceneManager.LoadScene(this.gameObject.name);
 }


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

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

250 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Most efficient way to transition between scenes? 1 Answer

SceneManager.LoadScene not working with button 1 Answer

I made an levelcontroller in c# but it always goes back to level 2 does someone know how to fix this? 2 Answers

How to only load one scene on app launch 0 Answers

How to set the position of an object in another scene? 1 Answer


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