Reputation: 81
I made a 3D game with Unity and C# and i don't know how to make a level menu with locked levels and unlock system.
https://i.sstatic.net/cgRqW.jpg
SelectLevel.cs (The menu with levels) :
using UnityEngine;
using UnityEngine.SceneManagement;
public class SelectLevel : MonoBehaviour
{
public void selectLevel()
{
switch (this.gameObject.name) {
case "Level01" :
SceneManager.LoadScene("Level01");
break;
case "Level02" :
SceneManager.LoadScene("Level02");
break;
}
}
}
LevelComplete.cs :
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelComplete : MonoBehaviour
{
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Upvotes: 1
Views: 3658
Reputation: 352
If you want the next level to unlock after the completion of the previous level you should make an array of all the levels in the game and mark each one as completed when they are finished. Based on that you can take the next level in the array and "unlock" it.
pseudo code:
int[] levels = new int[15]; // 15 levels in existance
if (level1completed == true)
{
levels[0] = 1; // set the valua to 1 if it has been completed
PlayerPrefs.SetInt("level1", levels[0]); // make a file called "level1" and give it a value of levels[0]
}
now where you have the code to load the level on click you should use an if statement to check if the previous level was completed
if (buttonlevel2clicked == true)
{
levels[0] = PlayerPrefs.GetInt("level1", 0); // retreive the int from the file and assign it to variable
if (levels[0] == 1) //checks if level 1 has been completed
{
level2.load(); //loads level 2
}
}
This code could be improved by not hard coding the level loading but this should give you an idea of what to do (i haven't tested it)
Playerprefs >> https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Upvotes: 0