PlayerPrefs Problem
So i have everything set up and it works but i'm getting a problem since even though it says that pointspersec=1; after I put the PlayerPrefs it becomes 0 ingame. Here's the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Click : MonoBehaviour {
public UnityEngine.UI.Text ppc;
public UnityEngine.UI.Text pointsDisplay;
public float points = 0;
public float pointsperclick = 1;
public void Start()
{
points = PlayerPrefs.GetFloat("points1");
pointsperclick = PlayerPrefs.GetFloat("points");
}
private void Update()
{
pointsDisplay.text = "Points: " + CurrencyConverter.Instance.GetCurrencyIntoString(points, false, false);
ppc.text = "Points / Click: " + CurrencyConverter.Instance.GetCurrencyIntoString(pointsperclick, false, true);
PlayerPrefs.GetFloat("points1", points);
}
public void Clicked()
{
points += pointsperclick;
PlayerPrefs.SetFloat("points", pointsperclick);
}
}
Answer by EvilGoat · Jun 27, 2017 at 02:02 PM
If you want to have the pointsperclick you assigned in the script, use PlayerPrefs.SetFloat("points", pointsperclick); in Start().
Right now you are using pointsperclick = PlayerPrefs.GetFloat("points"); in Start(), which loads the "points" value to pointsperclick (which is most likely 0).
I also assumed that you wanted to save the points, not the points per click in Clicked().
So the complete script should look like:
public class Click : MonoBehaviour {
public UnityEngine.UI.Text ppc;
public UnityEngine.UI.Text pointsDisplay;
public float points = 0;
public float pointsperclick = 1;
public void Start()
{
points = PlayerPrefs.GetFloat("points1");
PlayerPrefs.SetFloat("points", pointsperclick);
}
private void Update()
{
pointsDisplay.text = "Points: " + CurrencyConverter.Instance.GetCurrencyIntoString(points, false, false);
ppc.text = "Points / Click: " + CurrencyConverter.Instance.GetCurrencyIntoString(pointsperclick, false, true);
}
public void Clicked()
{
points += pointsperclick;
PlayerPrefs.SetFloat("points1", points);
}
Your answer
Follow this Question
Related Questions
Set the username with an inputfield ? 3 Answers
Why does my item change its cost to 1 whenever i buy it? 0 Answers
How to control an object after clicking on it? 1 Answer
High-score Saving Issue 2 Answers