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 jjdoughboy · Aug 26, 2020 at 08:52 PM · uimain menulevel select

how do I set up a level select scene in my game

I have Level select scene in my game. Where you press a button that will take you to that level but for some reason I get an error that says: "Cannot load scene: Invalid scene name (empty string) and invalid build index -1 UnityEngine.SceneManagement.SceneManager:LoadScene(String) LevelSelect:Select(String) (at Assets/AFA_TD/Scripts/UIScrpits/LevelSelect.cs:27) UnityEngine.EventSystems.EventSystem:Update()"

I currently have the level select script attached to the level select panel, and the level 1 button has the level select panel in the on click section of the button

How can fix my script so that when I press the button it will load the right level. I should note I currently have four levels and I want each button to load its corresponding level. I also have a main menu scene in which I have a level secret button that takes the player to the level select scene. I don't know this has anything to do with it but I'll put it here anyway.

Here is my level select script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class LevelSelect : MonoBehaviour
 {
     public string mainMenu = "MainMenu";
 
     public Button[] levelButtons;
 
     public void Start()
     {
         int levelReached = PlayerPrefs.GetInt("levelReached", 1);
         for(int i = 0; i < levelButtons.Length; i++)
         {
             if(i + 1 > levelReached)
             {
                 levelButtons[i].interactable = false;
             }
         }
     }
 
     public void Select(string level)
     {
         SceneManager.LoadScene(level);
         Time.timeScale = 1f;
     }
 
     public void MainMenu()
     {
         SceneManager.LoadScene(mainMenu);
     }
 
 }


Here is my Main menu script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class MainMenu : MonoBehaviour
 {
     public string levelSelect = "LevelSelect";
 
     public string levelToLoad = "Level01";
 
     public void Play()
     {
         SceneManager.LoadScene(levelToLoad);
         Time.timeScale = 1f;
     }
 
     public void LevelSelect()
     {
         SceneManager.LoadScene(levelSelect);
     }
 
     public void Quit()
     {
         /*Application.Quit();*/
         UnityEditor.EditorApplication.isPlaying = false;
     }
 }
 
 
Comment
Add comment · Show 2
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 Llama_w_2Ls · Aug 27, 2020 at 08:39 AM 0
Share

Have you actually assigned the scenes in the build settings? Since the errors are in loading scenes from the build settings.

avatar image catsareus123 · Aug 27, 2020 at 04:06 PM 0
Share

this is the code that I use for my main menu` public void PlayGame() { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1); }`

I then put the void into the OnClick trigger of the button.

the +1 should replaced with whatever you need to add to your build to get to the scene.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MatthewOCallaghan · Aug 26, 2020 at 09:13 PM

First glance suggests you may not be passing in the level name to your Select method. In the level button's OnClick, have you put the level name as a parameter?alt text

For example, here I pass "Menu" to my ChangeScene method in my Transition script.


capture.jpg (16.3 kB)
Comment
Add comment · 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 jjdoughboy · Aug 27, 2020 at 05:12 PM 0
Share

@$$anonymous$$atthewOCallaghan that did it very simple overlook on my part

avatar image MatthewOCallaghan jjdoughboy · Aug 28, 2020 at 01:27 PM 0
Share

Glad I could help. Could you accept the answer please?

avatar image
0

Answer by catsareus123 · Aug 26, 2020 at 09:27 PM

https://www.youtube.com/watch?v=zc8ac_qUXQY

this should help

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

Answer by Aviryx · Aug 26, 2020 at 11:17 PM

What you are doing doesn't make sense. You have a method Select() which takes a string in LevelSelect.cs but you never use it.


 public void Select(string level)
  {
      SceneManager.LoadScene(level);
      Time.timeScale = 1f;
  }

You need to store a reference and pass a string. (you also need to add the scenes to your build index)


 public class LevelSelect : MonoBehaviour
 {
     public void Select(string level)
     {
         SceneManager.LoadScene(level);
         Time.timeScale = 1f;
     }
 }

 public class MainMenu : MonoBehaviour
 {
     public LevelSelect levelSelect;

     void Start()
     {
         levelSelect.Select("SomeRandomScene");
     }
 }


You could also do it like this.


 public class MainMenu : MonoBehaviour
 {
     public Button level1;

     void Start()
     {
         // check if level has been reached, if not
         // level2.interactable = false;
         // etc

         level1.OnClick.AddListener(LoadLevel1);
     }

     private void LoadLevel1()
     {
     }
 }

A final note... player prefs are unsecure. They are exactly that; player preferences. They can be modified by the end user so they can just unlock all levels. If that isn't an issue for you that's fine but if you don't want players to be able to unlock levels by essentially editing a text file... check out the last half of this video about data persistence.


https://learn.unity.com/tutorial/persistence-saving-and-loading-data

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

228 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

Related Questions

UI buttons are clickable but not performing task after go back to main menu 1 Answer

Level Selection and Strange Error 0 Answers

Scenes overlap when using LoadLevel 0 Answers

How to add live background of your game to your main menu? 0 Answers

My game freezes 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