- Home /
Unable to get save score between levels working
I'm working on maze based game whereby the player moves around collecting tokens, which along with the distance travelled is added up to create a score, however I can't seem to get save score between levels to work, it just puts the score back to zero everytime. I'm using player prefs as recommended and followed a tutorial closely using the same code. What's odd is that it works for distance travelled on its own, just not score.
Heres my code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class PC1 : MonoBehaviour {
public Text countText;
public Text winText;
public Text timerText;
public Text scoreText;
public Text distanceText;
public int moveForce = 12;
float Score = 0;
public int seconds, minutes;
float distanceTravelled = 0;
Vector3 lastPosition;
public GameObject Player;
public GameObject other;
Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
lastPosition = transform.position;
if (PlayerPrefs.HasKey ("TotalScore"))
{
if (Application.loadedLevel == 0)
{
PlayerPrefs.DeleteKey ("TotalScore");
}
else
{
Score = PlayerPrefs.GetFloat ("TotalScore");
}
}
}
void FixedUpdate ()
{
Vector3 moveVec = new Vector3 (CrossPlatformInputManager.GetAxis ("Horizontal"), 0, CrossPlatformInputManager.GetAxis ("Vertical")) * moveForce;
rb.AddForce (moveVec);
}
void Update ()
{
minutes = (int)(Time.time/60f);
seconds = (int)(Time.time % 60f);
distanceTravelled += Vector3.Distance (transform.position, lastPosition);//other.transform.position
lastPosition = transform.position;
Score = distanceTravelled + count * 10;
scoreText.text = "Score = " + Score.ToString ("00");
timerText.text = "Time : " + minutes.ToString ("00") + ":" + seconds.ToString ("00");
distanceText.text = "Dist : " + distanceTravelled.ToString("f2");
}
void SaveScore()
{
PlayerPrefs.SetFloat ("TotalScore", Score);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 6) {
int i = Application.loadedLevel;
System.IO.File.AppendAllText ("G:/ScoresList.txt", Score.ToString() + "/new/");
SaveScore ();
Application.LoadLevel (i + 1);
}
}
}
I tried removing count = 0 in the Start but still doesnt do anything.
Any hep would be much appreciated.
Answer by ForeignGod · Mar 18, 2016 at 12:29 PM
PlayerPrefs.Save and remove count = 0 on Start aswell.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072121im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers
Saving skill allocations for characters 1 Answer
Currency System Not Saving!? 1 Answer
Multiple Cars not working 1 Answer
How to create log by PlayerPrefs? 1 Answer