Is there anyway to call on whenever a scene range loads to load a PlayerPrefs int key?

Is there anyway to call on whenever a scene range loads after start to load a PlayerPrefs int key? I am new to programming so I have to ask. Of course, this is a feeble basic attempt at what I am asking just to give people an idea of the simplicity I am going for at maintaining an integer that PlayerPrefs stores that is added to a score with each button click and with each score added a new scene in a random range loads.

 void Start()
   
    {

        PlayerPrefs.SetInt("scoreKey", scoreAmount);
        scoreAmount = 0;
        Score = GetComponent<TMP_Text>();
        PlayerPrefs.GetInt("scoreKey");
       
       
       
    }

You have partially the code already:

scoreAmount = PlayerPrefs.GetInt("scoreKey");

However, PlayerPrefs is not the ideal solution for this.
You should look into DoNotDestroyOnLoad.

I have tried that, I do not think really that could be right, I wish it was, things would be so much easier. Because the score is a TMPro Text box that can only be visible within a canvas and not at the root of the scene where it needs to be for DontDestroyOnLoad to work, I just cannot find a work around for that, how much easier it would be if I could.

I think you’re just not understanding how to properly use DontDestroyOnLoad, so you’re trying to create work-arounds for it like using PlayerPrefs to store data. In your previous thread you said you called it on TMPro Text component, which doesn’t seem right. You need to use DontDestroyOnLoad on the top-level GUI hierarchy so the entire GUI is not destroyed during load.

No shame in just using a static variable and being done with it.

There will only be one of them and they will maintain their value through a given run.

They will be reset when you restart your game but can be saved as well, such as via PlayerPrefs if you insist.

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

Useful for a relatively small number of simple values.

That is a really nice idea, I will give that a study and see how I can expand my knowledge on static variables and see if I can pull this off. Thanks alot!

I know, but I have things in the scene in the canvas that I do not want to carry over so I just do not think that would work for me.

I figured it out and I was surprised by how simple it was from all that I tried and it was a simple implementation of player prefs get and set, where to start saving the key from and where and when to continue to call on it per update, as simple as two lines.

void Start()
   
   
    {
       
        scoreAmount = 0;
        Score = GetComponent<TMP_Text>();
        scoreAmount = PlayerPrefs.GetInt("scoreKey");






    }




    void Update()

   
       
    {
       
        Score.text = scoreAmount.ToString();
        PlayerPrefs.SetInt("scoreKey", scoreAmount);
    }