- Home /
Question by
joaoruirego2 · Oct 28, 2020 at 01:35 AM ·
playerprefsadd
Add values with PlayerPrefs
I'm making a game, and I'm stuck in this problem. I want to add all the player distances to have a total of how many meters he has done.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Total : MonoBehaviour {
public static Total instance;
public float distancia;
public float total = 0;
public Text totalText;
public GameObject player_mm;
public Transform player;
private float x;
// Start is called before the first frame update
void Start()
{
if (instance == null)
{
instance = this;
}
total = PlayerPrefs.GetFloat("total");
totalText.text = PlayerPrefs.GetFloat("total").ToString();
distancia = PlayerPrefs.GetFloat("distancia");
total = x;
}
// Update is called once per frame
void Update()
{
total = x + distancia;
totalText.text = total.ToString();
distancia = player.position.z;
PlayerPrefs.SetFloat("distancia", distancia);
PlayerPrefs.SetFloat("total", total);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613014707im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is PlayerPrefs Not Saving? 1 Answer
Unity 4.6: 2 UI sliders updating will not work 2 Answers
How to remember the level/scene? 1 Answer
How to save dictionary keycode values to PlayerPrefs with C# 2 Answers
How to handle IAP refund in iOS? 0 Answers