Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved PlayerPrefs Help

Discussion in 'Editor & General Support' started by M3ntalll, Jul 1, 2020.

  1. M3ntalll

    M3ntalll

    Joined:
    Jul 1, 2020
    Posts:
    8
    I am working on a save/load system for my idle clicker game. However, if you launch the game and click load game, every item/multiplier will cost 0$ due to no player prefs previously being saved, therefore completely breaking the game if you click "Load Game" instead of "New Game" without previously playing the game and saving. Right now, I am saving every little variable & value in the game. (It is a skateboarding clicker game). What is the solution to this? Is it possible to check if PlayerPrefs are previously set before loading?

    Save Game Script:
    SaveGame.PNG

    Load Game script:
    GameLoading.PNG
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    To check whether a key already exists you can use PlayerPrefs.HasKey(key), and you can provide a default value: PlayerPrefs.GetInt(key, defaultValue)
     
    M3ntalll likes this.
  3. M3ntalll

    M3ntalll

    Joined:
    Jul 1, 2020
    Posts:
    8
    Figured it out! Thank you!