help please my level unlocks wrong when i rejoin the first level

So the first script is my c# that have control and the seccond c# is my enemy script where id does this :

Application.LoadLevel("TestLevel1");```
So my question is how can i do it good. Because now if i select Level1 for example and if Level2 is unlocked it unlocks Level3 and i don't wand that i wand if i play thro Level1 > Level5 That when i join a older level for example Level4 Level6 does not unlock. I now the +1 Means unlock 1 level even if i play for example level3 and then Level6 unlocks

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LevelManager1 : MonoBehaviour
{
    Button[] LevelButons;

    // Start is called before the first frame update
    private void Awake()
    {
        int ReachedLevel = PlayerPrefs.GetInt("ReachedLevel", 1);
        if (PlayerPrefs.GetInt("Level") >= 2)
        {

            ReachedLevel = PlayerPrefs.GetInt("Level");
        }

        LevelButons = new Button[transform.childCount];
        for (int i = 0; i < LevelButons.Length; i++)
        {
            LevelButons[i] = transform.GetChild(i).GetComponent<Button>();
            LevelButons[i].GetComponentInChildren<Text>().text = (i + 1).ToString();
            if (i + 1 > ReachedLevel)
            {
                LevelButons[i].interactable = false;
            }
        }
    }
    //loads all levels but you have to chanche the name from LoadLevel!

    public void LoadScene1(int Level)
    {
        PlayerPrefs.SetInt("Level", Level);
        Application.LoadLevel("Level1");
    }
   
    public void LoadScene2(int Level)
    {
        PlayerPrefs.SetInt("Level", Level);
        Application.LoadLevel("Level2");
    }
   
    public void LoadScene3(int Level)
    {
        PlayerPrefs.SetInt("Level", Level);
        Application.LoadLevel("Level3");
    }
   
    public void LoadScene4(int Level)
    {
        PlayerPrefs.SetInt("Level", Level);
        Application.LoadLevel("Level4");
    }
  
    public void LoadScene5(int Level)
    {
        PlayerPrefs.SetInt("Level", Level);
        Application.LoadLevel("Level5");
    }
   
    void Update()
    {

    }
}
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public class LevelController : MonoBehaviour
{
    private Enemy[] _enemies;
   
    private void OnEnable()
    {
        _enemies = FindObjectsOfType<Enemy>();
    }

    // Update is called once per frame
    void Update()
    {
        foreach(Enemy enemy in _enemies)
        {
            if (enemy != null)
                return;
        }
       
        Debug.Log("you killed all enemies");

        PlayerPrefs.SetInt("ReachedLevel", PlayerPrefs.GetInt("ReachedLevel") + 1);
        Application.LoadLevel("TestLevel1");
    }
}

Ideally get away from using this throughout your code:

 PlayerPrefs.SetInt("Level", Level);

because it makes a mess, and anywhere that “Level” is misspelled, such as “LEvel”, you’ll have a bug.

Instead make a static class like this:

using UnityEngine;
using System.Collections;

public static class SETTINGS
{
    // make one of these blocks for every variable you want to store
    private const string s_LevelString = "Level";
    public static int Level
    {
        get
        {
            return PlayerPrefs.GetInt( s_LevelString, 1);
        }
        set
        {
            PlayerPrefs.SetInt( s_LevelString, value);
            PlayerPrefs.Save();
        }
    }
}

That way in your code you just go:

SETTINGS.Level = 1; // at start of game


SETTINGS.Level++; // when the level goes up

and use it like any other variable.

Once you clean up the code like that, it becomes easier to reason about. You can then put in lots of Debug.Log() statements to print out these values and figure out where things are going wrong.

Debug.Log( "Update(): enemies all killed. Level is " + SETTINGS.Level);
4 Likes

tnx for helping me respect that you help everyone on these forums

1 Like