- Home /
Question by
unity_5060696D9D9E602C0338 · Mar 22, 2021 at 11:01 AM ·
c#unity 2dsavingloadlevelload scene
Level select based on game progress
Hello everyone. I`m new to unity and C# and stuck here. So I am making new game 2D (simple one) I made few levels where you can pass level if u kill all monsters. After that I would like to save progress for example if i make it to level 5 I want to all levels before to be unlocked. Sorry if my question is stupid.
I would appreciate it if someone could help me or refer me to a similar topic.
public class LevelManager : MonoBehaviour
{
public Button[] buttons;
int levelIsUnlocked;
void Start()
{
levelIsUnlocked = PlayerPrefs.GetInt("levelIsUnlocked", 1);
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].interactable = false;
}
for (int i = 0; i < levelIsUnlocked; i++)
{
buttons[i].interactable = true;
}
}
public class LevelController : MonoBehaviour
{
[SerializeField] string _nextLevelName;
Monster[] _monsters;
void OnEnable()
{
_monsters = FindObjectsOfType<Monster>();
}
void Update()
{
if (MonsterAreAllDead())
{
GoToNextLevel();
}
}
void GoToNextLevel()
{
Debug.Log("Go to next level" + _nextLevelName);
}
private bool MonsterAreAllDead()
{
foreach (var monster in _monsters)
{
if (monster.gameObject.activeSelf)
return false;
}
return true;
}
}
Comment