- Home /
My levels don't unlock.
Scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ChooseLevelScript : MonoBehaviour
{
public Button[] levelbuttons;
int SaveLevel;
void Start()
{
int Levelpassed = PlayerPrefs.GetInt("Levelpassed", 1);
for (int i = 0; i < levelbuttons.Length; i++)
{
if(i + 9 > Levelpassed)
{
levelbuttons[i].interactable = false;
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FinishTileScript : MonoBehaviour
{
int Levelpassed;
int sceneIndex;
void Start()
{
sceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
Levelpassed = PlayerPrefs.GetInt("Levelpassed");
}
void Update()
{
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (PlayerPrefs.GetInt("Levelpassed") < sceneIndex)
{
PlayerPrefs.SetInt("Levelpassed", sceneIndex);
SceneManager.LoadScene("LevelSelection");
}
}
}
My problem is that my levels don't unlock, And level 1 locks, I don't know why.
there are 10 levels with 10 buttons, and the buildindex of level 1 is 2.
Answer by EyadThedevelopingBoi · Feb 01, 2020 at 08:39 PM
So I fixed the problem!
I forgot to put the script on the object! Whoops!
Answer by logicandchaos · Feb 01, 2020 at 07:25 PM
Why are you adding the +9 in this line: if(i + 9 > Levelpassed) right now you have if Levelpassed is less than 9 level one will lock, if Levelpassed is less than 10 level two will lock. I don't see anywhere in your code where you set levelbuttons[i].interactable = true; so that would be why they don't get activated. a small note Levelpassed should be levelPassed for proper naming convention.
Your answer
Follow this Question
Related Questions
How to make buttons have sound when it is highlighted and clicked? 0 Answers
Add Listeners to array of Buttons 2 Answers
How To Put 2 Panel in 1 scene? 2 Answers
How to Load PlayerPrefs from a different scene 1 Answer
I can't click to an UI button. 3 Answers