I have a variable gem_number with gets from another script (also the game object is in a different scene) a number. I am trying to add the number to trueGems variable, display it on the screen and save the number when exiting the game. The problems are: the gems don’t save when exiting the game and reset when reentering in this scene instead of adding new value.
using UnityEngine;
public class Gem_show : MonoBehaviour
{
public TMPro.TextMeshProUGUI Gems;
public static int gem_number; //get the number of gems earned
private int trueGems; //the total of gems stored
private void Start()
{
trueGems += gem_number; //add to the current gems more gems
PlayerPrefs.SetInt("Gem", trueGems); //don't lose the gems when exit the game
Gems.text =PlayerPrefs.GetInt("Gem").ToString(); //display gems
}