- Home /
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;
}
}
Have you actually assigned the scenes in the build settings? Since the errors are in loading scenes from the build settings.
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.
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?
For example, here I pass "Menu" to my ChangeScene method in my Transition script.
@$$anonymous$$atthewOCallaghan that did it very simple overlook on my part
Glad I could help. Could you accept the answer please?
Answer by catsareus123 · Aug 26, 2020 at 09:27 PM
https://www.youtube.com/watch?v=zc8ac_qUXQY
this should help
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