- Home /
How to link buttons to individual levels within the same scene?
Hi,
I'm using Unity to build a game but I want to add a start screen to it, with buttons for Level 1, Level 2, Level 3 and Level 4. I want the buttons, once pressed on, to go straight to that level and if the player passes it, to continue on with the next randomly generated levels. (I'm basing the game off the Unity 2D Roguelike Tutorial)
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement; // Allows SceneManager to be used.
public class LoadSceneOnClick : MonoBehaviour
{
public void LoadByIndex(int sceneIndex) // Passes in what scene will be loaded.
{
SceneManager.LoadScene(sceneIndex); // SceneManager loads scene.
}
}
This is my code for my LoadSceneOnClick script that I am using for each of my main menu buttons. I got this from the Creating A Main Menu tutorial. However it is not working because it assumes that each level is a different scene. This game contains all its levels in a single scene so I'm not sure how to make this work. I've tried passing in the variable for level instead of sceneIndex but it did not work, so maybe I was not doing it right.
Any help would be appreciated!!
Thanks :)
what kind of varable do you want to pass? (I'ver never done the Roguelike Tutorial). Is it some position in world space where your player is supposed to be?
I want to pass a variable that stores an integer value that is what level I want the game to start at (like how I'm doing it in the picture).
You can look into scriptable Objects ( https://unity3d.com/de/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects ) or create a game $$anonymous$$anager based on the singleton pattern witho DondtDestroyOnLoad (http://wiki.unity3d.com/index.php/Singleton ) . I personally prefer the first option.
Answer by jwulf · Jan 13, 2017 at 12:29 PM
I don't know what the Roguelike Tutorial is, but if you have multiple Levels in one Scene, you could achieve what you want to do like:
public void LoadByIndex(int levelIndex)
{
PlayerPrefs.SetInt("initialLevel", levelIndex); // Store which level in the loaded scene will be played
SceneManager.LoadScene("yourSceneName"); // SceneManager loads scene.
}
And then in your scene with the 4 levels, have some sort of LevelController, which (e.g. in the Start()-Method) does something like:
int i = PlayerPrefs.GetInt("initialLevel");
// TODO start the level identified by i --> Set Player's initial position to level i's position, spawn enemies in level i, etc...
PlayerPrefs should never be used for important data such like level progress (as far as I understand from breadtop description a new level gets unlocked if the player passes the previous one and you don't want the player to manipulate it that easy)
True, but I thought, the 4 levels are all supposed to be available from the beginning and when one is passed, a randomly generated level is started.
If it should be safe from manipulation, storing a serialized class in the PersistentDataPath would be better.
Good to know! :)
Please take L$$anonymous$$84's objection into account that using the PlayerPrefs is not safe from manipulation by a user, if that's a problem you want to consider in your use case.
Your answer
![](https://koobas.hobune.stream/wayback/20220612104717im_/https://answers.unity.com/themes/thub/images/avi.jpg)