save unlocked character by Playerprefs

hello. I have a code in which when you click on a button and you have enough coins, it must be unlocked and a piece of wood with price on it that acts like a lock must be disappear. Every thing works fine, but when I restart the game, every thing returns to default. I’m new to unity and I want to use Playerprefs but I don’t know how exactly. here is my script;

    using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        using UnityEngine.SceneManagement;
        using UnityEngine.UI;
        
        public class MainMenu3 : MonoBehaviour {
        public Button WhitePlane;
        public Button BluePlane;
        public static int character_number;
        
        public GameObject wood1;
        public GameObject wood2;
        
        void Start () {
    
        character_number = PlayerPrefs.GetInt ("Number");
        BluePlane.onClick.AddListener (() => {
        character_number=1;
        if (UIManager2.coin_score>=1) {
        UIManager2.coin_score--;
        wood1.setactive (false);
        
        SceneManager.LoadScene ("Menu2");
        			}
        
        		});
        		
       WhitePlane.onClick.AddListener (() => {
        character_number=2;
        if (UIManager2.coin_score>=2){
        				
        UIManager2.coin_score--;
        UIManager2.coin_score--;
        wood2.setactive(false);
        
        SceneManager.LoadScene ("Menu2");
                                  }
        			});
    void Update () {
    PlayerPrefs.SetInt ("Number", character_number);
    }

You are on the right path, only that the PlayerPrefs.SetInt (“Number”, character number); must be immediately after the value character number is setted, not in the Update loop

Thanks. I now understand that I should have set a boolean for lock/unlock and also for Playerprefs. I really don’t know what’s wrong! It doesn’t work yet! Below is my original script. BluePlane is already unlocked by default and UIManager2.coin_score refers to number of coins to be saved and loaded after buying any item. I thought it might be something wrong with this piece of code but nothing changed after I erased that.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.SceneManagement;
  using UnityEngine.UI;
            
  public class MainMenu3 : MonoBehaviour {
  public Button WhitePlane;
  public Button BluePlane;
  public static int character_number;
            
  public GameObject wood1;
    
  public bool WhitePlaneUnlocked;

            
  void Start () {
        
  character_number = PlayerPrefs.GetInt ("Number");
  WhitePlaneUnlocked = PlayerPrefs.GetInt ("WhitePlaneUnlocked") == 0 ? false:true;

  wood1.SetActive (WhitePlaneUnlocked);

            
  BluePlane.onClick.AddListener (() => {
  character_number=1;
  SceneManager.LoadScene ("Menu2");
            			}
            
            		});
  UIManager2.coin_score = PlayerPrefs.GetInt ("Score");		
  WhitePlane.onClick.AddListener (() => {
  character_number=2;
  PlayerPrefs.SetInt ("Number", character_number);
  if (UIManager2.coin_score>=2){
            				
  UIManager2.coin_score-=2;
  PlayerPrefs.SetInt("WhitePlaneUnlocked",1);
  wood1.setactive(false);
            
  SceneManager.LoadScene ("Menu2");
                                      }

            			});

As far as I know, if statement for number of coins must be terminated once it’s character selected. I thought assigning a variable could be helpful. I tried many ways but none of them worked! I really don’t know what to do.
@Jasr_88

The problem with your code is that you have not assigned a default value to your PlayerPref. Often PlayerPrefs behaves weird when they are not assigned with a default value. So here is the solution to your problem:

In Start function, just replace

character_number = PlayerPrefs.GetInt ("Number");

with

if(!PlayerPrefs.HasKey ("Number")) {
   PlayerPrefs.SetInt ("Number", defaultValue);
}
character_number = PlayerPrefs.GetInt ("Number");

Replace defaultValue with your desired default value.

Hopefully, this small change will help you solve your problem. Now PlaayerPref will initialize when running for the first time and after that will never show weird behavior.

Note: After applying these changes, Go to Edit and click Clear All PlayerPrefs, and then run the program to test whether changes worked or not.

Enjoy… !!! :wink: