Issue at saving int values.

I’ve set up a script which saves position and two int values (xp and rare objects collected)

When loading, I manage to get the position which I saved before loaded. However, that isn’t happening with the int values. I want both int values (which can be seen via GUI) to be shown as I saved them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class leveluprare : MonoBehaviour {

    static int RareObjectCount;
    static int xp = 0;

    void Start () {
    }
   
    // Update is called once per frame
    void Update () {
       
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.name == "Rare")
        {
            RareObjectCount += 1;
            xp += 100;
            Destroy(other.gameObject);
        }

        if (other.name == "Punto")
        {
            xp += 50;
            Destroy(other.gameObject);
        }
    }

    private void OnGUI()
    {
        GUI.Label(new Rect(10, 20, 100, 20), "XP = " + xp);
        GUI.Label(new Rect(10, 65, 100, 20), "Rare = " + RareObjectCount);
    }

   public void Save()
    {
        PlayerPrefs.SetFloat("PlayerX", transform.position.x);
        PlayerPrefs.SetFloat("PlayerY", transform.position.y);
        PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
        PlayerPrefs.SetInt("Rare", RareObjectCount);
        PlayerPrefs.SetInt("XP", xp);
    }

    public void Load()
    {
       float x = PlayerPrefs.GetFloat("PlayerX", transform.position.x);
       float y = PlayerPrefs.GetFloat("PlayerY", transform.position.y);
       float z = PlayerPrefs.GetFloat("PlayerZ", transform.position.z);
       int RareObjectCount = PlayerPrefs.GetInt("Rare");
       int xp = PlayerPrefs.GetInt("XP");

        transform.position = new Vector3(x, y, z);

    }
}

You are declaring new local variables with the same name as your static variables here:

       int RareObjectCount = PlayerPrefs.GetInt("Rare");
       int xp = PlayerPrefs.GetInt("XP");

To fix it just assign the PlayerPref values to your existing static variables instead:

       RareObjectCount = PlayerPrefs.GetInt("Rare");
       xp = PlayerPrefs.GetInt("XP");

It works. Thank you!