Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Saving Data With PlayerPrefs Help

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Feb 22, 2020.

?

What save data gimmick do you use mostly?

  1. Simple retro game style like PlayerPrefs

    0 vote(s)
    0.0%
  2. Complex save files

    1 vote(s)
    100.0%
  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    I have made an update to my Alpha Build which I have announced over social media. One thing though. I have no idea how to fix the reset button. At the time, I have made a golden target prefab, and the scripts that of course which if the target is destroyed and that the level is completed. You would find golden text of the level you beat. The problem is getting it to work. Take a look at the scripts I conjured up, and tell me how much I'm missing:

    Golden Target Script:
    Code (CSharp):
    1. public class GoldTarget : MonoBehaviour
    2. {
    3.     public int targetNum = 0;
    4.  
    5.     void OnTriggerEnter(Collider other)
    6.     {
    7.         if (other.gameObject.CompareTag("Blast"))
    8.         {
    9.             targetNum += 1;
    10.             Destroy(gameObject);
    11.          
    12.             if(targetNum == 1)
    13.             {
    14.                 GoldText.TargetOneIsDestroyed = true;
    15.             }
    16.  
    17.             if(targetNum == 3)
    18.             {
    19.                 GoldText.TargetTwoIsDestroyed = true;
    20.             }
    21.  
    22.             if(targetNum == 5)
    23.             {
    24.                 GoldText.TargetThreeIsDestroyed = true;
    25.             }
    26.         }
    27.     }
    28. }
    Gold Text Script:
    Code (CSharp):
    1. public class GoldText : MonoBehaviour
    2. {
    3.     public static bool TargetOneIsDestroyed;
    4.     public static bool TargetTwoIsDestroyed;
    5.     public static bool TargetThreeIsDestroyed;
    6.     public GameObject White1;
    7.     public GameObject Gold1;
    8.     public GameObject White2;
    9.     public GameObject Gold2;
    10.     public GameObject White3;
    11.     public GameObject Gold3;
    12.     int ProgressA = 0;
    13.     int ProgressB = 0;
    14.     int ProgressC = 0;
    15.  
    16.     void Start()
    17.     {
    18.         data();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(TargetOneIsDestroyed == true && ScoringSystem.theScore == 0)
    25.         {
    26.             White1.SetActive(false);
    27.             Gold1.SetActive(true);
    28.             PlayerPrefs.SetInt("ProgressA", 1);
    29.         }
    30.  
    31.         if (TargetTwoIsDestroyed == true && ScoringSystem.theScore == 0)
    32.         {
    33.             White2.SetActive(false);
    34.             Gold2.SetActive(true);
    35.             PlayerPrefs.SetInt("ProgressB", 1);
    36.         }
    37.  
    38.         if (TargetThreeIsDestroyed == true && ScoringSystem.theScore == 0)
    39.         {
    40.             White3.SetActive(false);
    41.             Gold3.SetActive(true);
    42.             PlayerPrefs.SetInt("ProgressC", 1);
    43.         }
    44.     }
    45.  
    46.     void data()
    47.     {
    48.         PlayerPrefs.GetInt("ProgressA");
    49.         PlayerPrefs.GetInt("ProgressB");
    50.         PlayerPrefs.GetInt("ProgressC");
    51.     }
    52.  
    53.     public void Reset()
    54.     {
    55.         White1.SetActive(true);
    56.         Gold1.SetActive(false);
    57.         White2.SetActive(true);
    58.         Gold2.SetActive(false);
    59.         White3.SetActive(true);
    60.         Gold3.SetActive(false);
    61.         PlayerPrefs.DeleteKey("ProgressA");
    62.         PlayerPrefs.DeleteKey("ProgressA");
    63.         PlayerPrefs.DeleteKey("ProgressA");
    64.     }
    65. }
    One more thing to mention: Every time I clear a level and go back to the main menu, the reset button doesn't want to work. Anything that I missed? Lemme know. Any help is appreciated.
     
    Last edited: Feb 22, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    In your second script, the calls inside the
    data()
    function do nothing with the integers they get. I think you want to assign them to your variables by the same name. That won't happen automatically, you have to assign the return value to the variable.
     
  3. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Got it. Now for the reset button. Is that the function I need to set the return value?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I'm not sure I understand that question exactly.

    But I want to point out that while you CAN name a function as Reset, in this case it is actually also a reserved function only used in the Unity Editor. If you look at a monobehavior in the inspector, on the right side upper corner of its inspector sub-area there is a pulldown that has Reset as one of its options. That pulldown calls the Reset() function. Here are the docs on it:

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html

    It is an editor-only mechanism, so at runtime it will work only when you call it explicitly. I still however recommend renaming your Reset() function to maybe ResetProgress() or something, just to eliminate future confusion.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    oh ok, so he did end up confused after all.
    sorry Kurt-Dekker for unintentionally calling you a troll, here :)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I certainly do not troll, especially not in a technical forum. I am a professional developer. I do this for a living and I enjoy helping others get better at Unity. Now I am not above using light humor in my posts, and I can certainly misunderstand a poorly-phrased question, but I absolutely do not troll people.
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    oh dear, you have clearly misunderstood my message here

    [edit]
    Let me explain this better, perhaps today is not your day.

    Someone comes along and says "someone told me to put this return here"
    And I've told him that whoever said to put a return there was trolling him, partly humorously.
    And now I've, also party humorously, apologized to you for calling you "a troll" after I've seen the original messaging between you two.

    It's complicated only because of the paraphrasing going on, and multiple threads involved, and I hope you can tell that I definitely do not mean that you're a troll, nor I'm accusing you of it. It was just an attempt to clear any misunderstanding.

    Btw, if it helps, I've known you for quite a while on these forums. I have lurked for at least a decade without being active.
     
    Last edited: Feb 23, 2020