So recently I’m developing my first game and I really got so far progress by now… and now I’m stucked at this point; I made a level selection menu and I have 3 levels in the game for now and only 1st one unlocked the other 2 are locked… I have this code in my level selection menu to lock other levels except first level and I need to know what to do now, I really need help… I watched a lot of tutorial videos like 321312 videos but i got so confused and didnt know how to get those codes and adapt them to my script am I going to improve this code or write another script file to add another component i really need help if i can complete this LEVEL UNLOCK system i can get more progress on my game and i ll really get so close to finish and publish my first game PLS HELP ME
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class levelSelector : MonoBehaviour
{
public Button[] levelButtons;
void Start()
{
int levelReached = PlayerPrefs.GetInt("levelReached", 1);
for (int i = 0; i < levelButtons.Length; i++)
{
if (i + 1 > levelReached)
levelButtons[i].interactable = false;
}
}
I’ve read your post 3 times and I still can’t find what specifically you are asking for. Instead of making your entire post bold and the title in all caps, just write a clear and specific question.
The code you have posted looks like it is from a level unlock system. So what’s the problem specifically?
what do you mean from unluck system but it doesnt unlock next levels something missing but idk im very new at programming and developing games and i need help
Can you show us your code where you are calling PlayerPrefs.SetInt to set “levelReached”? Your code unlocks levels based on the saved value of levelReached in PlayerPrefs. It defaults to 1 if you have never saved levelReached before (new player for example).
If you don’t have that anywhere, well that is the issue.
It might be best to go look up some Youtube tutorials and go from one of those. It is not just a “simple thing” for us to engineer you a solution, since there are plenty of ways to do it and many people have already published it for you to study.
So there is a feature of Unity called PlayerPrefs. It saves values to disk (or Windows to the registry). On line 11 of the code you posted you are using that feature to read back the value of key “levelReached”. That only makes any sense if you also are saving a value to that key somewhere else in your code. To save a value to that key you use PlayerPrefs.SetInt. See the link in my previous comment.
You’ll have to add that somewhere in your game, wherever you want to add level unlocks, or line 11 will never return anything but 1.