- Home /
Trying to save and load scores from PlayerPrefs
Hello. I'm trying to save and load total points player has got from PlayersPrefs, it works fine so far until next level starts. Total points stays at 0, even though it should show 4000 points after first level if you dont fail anything.
Heres the script thats supposed to load score from PlayerPrefs:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class BoxCol : MonoBehaviour
{
public GameObject[] boxes;
public int Points;
public static int totalPoints;
void Start()
{
totalPoints = PlayerPrefs.GetInt("TotalPoints");
}
void Update()
{
boxes = GameObject.FindGameObjectsWithTag("Box");
Debug.Log("Boxes: " + boxes.Length);
Points = boxes.Length * 1000;
if (Points == 0)
{
restartCurrentScene();
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Box")
{
Debug.Log("Box hit the ground");
Destroy(col.gameObject);
}
}
void OnGUI()
{
GUI.Box(new Rect(Screen.width / 2, 30, 200, 20), "Points: " + Points);
GUI.Box(new Rect(Screen.width / 2, 60, 200, 20), "Total Points: " + totalPoints);
}
public void restartCurrentScene()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
}
And heres the script thats supposed to save that total points score to PlayerPrefs, which it is supposed to get from that script I posted first.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Finish : MonoBehaviour {
public BoxCol totalPoints;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Truck")
{
Debug.Log("Finish line");
saveScores();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
public void saveScores()
{
PlayerPrefs.SetInt("TotalPoints", BoxCol.totalPoints);
}
}
I think it has something to do with loading it from another script as I can't put my "BoxCol.cs" script to the place on Inspector that says "BoxCol".
So any ideas how to fix it?
I think it has something to do with loading it from another script as I can't put my "BoxCol.cs" script to the place on Inspector that says "BoxCol".
Not sure what that means. If you're trying to assign a GameObject to the totalPoints field of a Finish component in the inspector, and it's not letting you, then you probably don't have a BoxCol component on the GameObject.
But I don't think that should matter because you're not using that field anywhere.
I can't actually see anywhere in the code you've shown, where you're modifying the value of BoxCol.totalPoints. You load it from PlayerPrefs and save it to PlayerPrefs, and that's all. So why are you expecting it to change?
Yes I noticed that itself too and added there a part:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Truck")
{
BoxCol.Points = BoxCol.totalPoints;
Debug.Log("Finish line");
saveScores();
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
}
}
But still no luck.
That's still not changing the value of BoxCol.totalPoints. And I wouldn't expect it to compile because BoxCol.Points is a member field and you're trying to access it as if it's static (like totalPoints is).
Sry if I'm being stupid, but doesnt BoxCol.Points = BoxCol.totalPoints tell it that current points are totalPoints when gameobject with tag: Truck hits the trigger?
The point is that that line changes the value of BoxCol.Points, not the value of BoxCol.totalPoints. And BoxCol.totalPoints is what's used in all your PlayerPrefs Set and Get calls. So that's what you need to change (before you call PlayerPrefs.SetInt using it) if you want to change what's stored in PlayerPrefs.
Well even if I try BoxCol.totalPoints = BoxCol.Points its still no luck.
After reviewing your code more, my first reply may of been hasty for a couple of reasons. First of all, are heading down a very very bad road. If you get comfortable writing code like this, it will be nothing but bad for you. Do your best to stop "Finding" objects, or $$anonymous$$imize it to OnEnables/$$anonymous$$ethods. There is NOTHING good about Finding objects by Tag or by Name. Despite popular belief. Also, using PlayerPrefs for saving data is NOT suggested, but doable, and at best it should only be done for small amounts of data, and done the proper way.
GameObjects can't be serialized, as such, no data of events should be stored inside a mono script, ins$$anonymous$$d passed to a scriptable object, or a static reference class. Even though playerPrefs is an outside source, you are still treating it like a local thing.
Reloading your scene as a way to restart is a decent method of doing so, but you should utilize the OnEnable/OnDisable methods that you have access to. Honestly the Start method is nearly useless.
You should also look into the 4.6 UI, as it makes OnGUI completely obsolete, and can still be instantiated during runtime, more effectively I might add, with prefabs.
Anyway, I hope some of this helped.
Answer by OmerDoron · Feb 12, 2017 at 08:28 PM
add totalPoints = PlayerPrefs.GetInt("TotalPoints"); on start at 2nd script
Answer by ayrrik · May 22, 2017 at 08:34 PM
@reefer If you are still having trouble with this, I can help - there's a few things I see wrong here, but don't want to get into it too far if you have already fixed your issue by now...
One big issue I see is where you added "BoxCol.Points = BoxCol.totalPoints;" (found in comments from first post).. you are referring to the BoxCol static value, but you also have a local instance of a BoxCol, which you named "totalPoints". So this line should be more like totalPoints.Points = totalPoints.totalPoints.. so your naming convention is a little funky or you need to remove the instance of BoxCol, and just make Point static as well.
Another issue is I don't see where you are adding or removing points for anything.. Maybe you are doing it in another script or maybe it should be in the "OnCollisionEnter" of BoxCol.. something like: totalPoints -= 1000, or Points -= 1000... or += instead of -=, depending on what you are trying to do here.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110920im_/https://answers.unity.com/themes/thub/images/avi.jpg)