- Home /
Unlock 2 levels which unlocks separate levels.
Imagine a map has more than one path, one level unlocks multiple levels that are on separate paths. And after they completed, each level unlocks the next level on their own paths. Can anyone help =) I found tutorials about how to unlock the next level. The problem is they created as to unlock the current and all the levels before them. Because of my level map is not a single linear system, those won't work for me.
@Orange-Pixell Can you please show us the relevant code you already have?
Sure
public Button[] lvlButtons;
void Start()
{
int levelReached = PlayerPrefs.GetInt("levelReached", 1);
for (int i = 0; i < lvlButtons.Length; i++)
{
if (i + 1 > levelReached)
lvlButtons[i].interactable = false;
}
}
The thing is that I have to put scenes into build in an order, I upload the levels that on one path from 1 to 5 and the levels on the other path from 6 to 11 so, if player unlocks one level on the 2nd path because that "i" will be 6 and all the levels on the first path will unlock.
I want the player to unlock levels one by one on the each path.
Answer by JPhilipp · Mar 15, 2020 at 04:42 PM
Think of your level unlocks as unlock ranges. Range 0 unlocks 1-5, Range 1 unlocks 6-10 and so on:
using UnityEngine;
public class LevelUnlocking : MonoBehaviour
{
// Define your own ranges here. Add more as you like.
int[][] unlockRanges =
{
new [] {1, 2, 3, 4, 5},
new [] {6, 7, 8, 9, 10}
};
void Start()
{
SetLevelButtonInteractable();
}
void SetLevelButtonInteractable()
{
for (int i = 0; i < unlockRanges.Length; i++)
{
string key = "RangeReached" + i;
bool rangeReached = PlayerPrefs.GetInt(key, 0) == 1;
if (rangeReached)
{
for (int j = 0; j < unlockRanges[i].Length; j++)
{
int level = unlockRanges[i][j];
// Make sure lvlButtons is defined for you.
lvlButtons[level].interactable = true;
}
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612224923im_/https://answers.unity.com/themes/thub/images/avi.jpg)