Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I want to save some PlayerPrefs and every Time i want to load the Float set it to 0

Discussion in 'Scripting' started by Goodkill1337, Feb 1, 2021.

  1. Goodkill1337

    Goodkill1337

    Joined:
    Feb 1, 2021
    Posts:
    4
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class scr_Save : MonoBehaviour
    {
    public Text load;
    // Start is called before the first frame update

    public void Save(float counterNumber)
    {
    PlayerPrefs.SetFloat("Variable",counterNumber);
    PlayerPrefs.Save();
    }


    public void Load()
    {
    load.text = PlayerPrefs.GetFloat("Variable") + " Sins";
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  3. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    If you want to set the float to 0 whenever you load it, what exactly is the point in storing it in the first place?
     
    Joe-Censored and Kurt-Dekker like this.
  4. Goodkill1337

    Goodkill1337

    Joined:
    Feb 1, 2021
    Posts:
    4
    I don´t want the 0 I always get the 0
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Either look into the ready-made property aliases I listed in the pastebin link above (they work for sure), or else to debug your existing code, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.