PlayerData, first time loading scene

Hi all,

I am having a problem with a script, atm I have a script that declare certain playerdatas (lives, score etc…):

public static int Lives;
public static int firstRun;
etc...

Now, the reason why I am storing this “firstRun” is that I have a menu with all the levels, in the form of buttons.
It works in a way that, if I reached level 1 at best, I can’t go to menu and select level 2, if I reached max level 3, I can’t go to menu and select level 4, etc…but the problem is, I set the playerprefs everytime I move from a scene to another, and it happens that, for example, you reached level 4, go to menu, you go back to level 1, from level 1 you reach level 2, and it save level 2 as your “max”.
So I thought I could make a script that check if it’s the first time you reach certain scene (atm you move from a scene to another with a trigger collider on a door). The script is supposed to, if it’s your first time reaching a certain level, to set playerprefs to the new level you reach, but if you already went into that level, it simply load the new level and doesn’t save anything (and so it keeps you last saving, which is the highest level you ever reached).
Here is the “corrupted” script :

 public void Start()
    {
        PlayerData.firstRun = PlayerPrefs.GetInt("firstRun");
    }
   void OnCollisionEnter2D(Collision2D coll)
  if (coll.collider.gameObject.tag == "tutorial_platform1")
 if (PlayerData.firstRun == 0)
            {
                PlayerPrefs.SetInt("levelReached", 2); //storing index
                SceneManager.LoadScene("tutorial_2");
                PlayerPrefs.SetInt("firstRun", 2); //I set it to 2 but any number different from 0 is fine, as long as is it is different from 0.
            }
        else
        {
          SceneManager.LoadScene("tutorial_2");
            }

Any help would be greatly appreciated.
Thanks in advance

Uhh…I closed Unity, took a few minutes break, re-opened Unity and now it works perfectly…:hushed:
Sorry guys, the problem got fixed by itself lol. Maybe I forgot to save something tho I was sure everything was saved :face_with_spiral_eyes:

Well, I’m not sure about all your code but you could write something like

PlayerPrefs.SetInt("levelReached",Mathf.Max(2,PlayerPrefs.GetInt("levelReached"));

That being said, you should not modify the player prefs constantly. Maybe load them at star, store them somewhere, change them and at the end of the game save them.

1 Like

Hmm…but why? Because of performance?

Well ,it’s mostly guess work that performance would be an issue but mostly the problem is that the code is not as clean.

saveData.levelReached = 2; looks a lot better than PlayerPrefs.SetInt(“levelReached”, 2);

Also, given the fact you want to use those for saves… you should probably use something else like custom binary files. PlayerPrefs can be modified outside the game with a text editor. They are meant for stuff like options settings.

I’m not so sure that the code you posted will keep working as you unlock levels (and if you were to go back n play an earlier level).
Unless you have a unique script like that for each level, the levels are all hard-coded.

If you finish a level, check if that level is greater than your highest unlocked level, and if it is , make it your new highest. Otherwise, you ignore it. I think that will always work well for you. Plus, I do not think you will need that firstRun variable, unless it is doing something else, also.

Just thought I’d post this in case it’s helpful :slight_smile: Enjoy your game.

i have faced a problem that when i started game then playerprefs did not start level2 first time but when i play level1 again then level2 open and soo on what should i do anyone help