Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by justinmed, Apr 1, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    100
    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.

    Code (CSharp):
    1.  void Start()
    2.    
    3.     {
    4.  
    5.         PlayerPrefs.SetInt("scoreKey", scoreAmount);
    6.         scoreAmount = 0;
    7.         Score = GetComponent<TMP_Text>();
    8.         PlayerPrefs.GetInt("scoreKey");
    9.        
    10.        
    11.        
    12.     }
     
    Last edited: Apr 1, 2024
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
  3. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    100
    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.
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    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.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    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:

    https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

    Useful for a relatively small number of simple values.
     
  6. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    100
    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!
     
  7. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    100
    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.
     
  8. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    100
    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.
    Code (CSharp):
    1. void Start()
    2.    
    3.    
    4.     {
    5.        
    6.         scoreAmount = 0;
    7.         Score = GetComponent<TMP_Text>();
    8.         scoreAmount = PlayerPrefs.GetInt("scoreKey");
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.     }
    16.  
    17.  
    18.  
    19.  
    20.     void Update()
    21.  
    22.    
    23.        
    24.     {
    25.        
    26.         Score.text = scoreAmount.ToString();
    27.         PlayerPrefs.SetInt("scoreKey", scoreAmount);
    28.     }
    29.  
    30.