- Home /
PlayerPrefs is acting weird ??
The first Script which calculates score and stores in Playerprefs.
using UnityEngine;
using System.Collections;
public class Scores : MonoBehaviour {
private int score;
// Use this for initialization
void Awake () {
PlayerPrefs.SetInt("score",0);
}
public void AddScore()
{
score = score + 1;
PlayerPrefs.SetInt("score", score);
Debug.Log(score);
}
}
Second Script which calls this first script for updating score.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Scores))]
public class TheCollision : MonoBehaviour {
public Pool pool;
public Poolb poolb;
private Scores scores;
void Start()
{
scores = GetComponent<Scores>();
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "moving")
{
poolb.deActivate(gameObject);
pool.deActivate(other.gameObject);
Debug.Log("Hitting the moving object");
scores.AddScore();
}
if(other.tag == "still")
{
poolb.deActivate(gameObject);
Debug.Log("Hitting the Still object");
Application.Quit();
}
}
}
And there is this third script which displays the score on canvas UI text
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplayScore : MonoBehaviour {
Text txt;
// Use this for initialization
void Start () {
txt = gameObject.GetComponent<Text>();
}
// Update is called once per frame
void Update () {
txt.text="Score : " + PlayerPrefs.GetInt("score");
}
}
BUT WHILE PLAYING MY SCORE RESETS BACK TO 0 SOMETIMES. SOMETIMES DROPS TO ANY RANDOM NUMBER NUMBER 4 2.
I dont know what is the problem here.
And please write a proper mechanism to update a score and access it. I guess my method is too naive and error prone.
PlayerPrefs should only be accessed when actually needed, not every frame. You should have a score variable in memory, which is written to disk with PlayerPrefs at an appropriate time (such as quitting the game).
so you mean that whatever calculations or counting i want to do i should do it with variable newly created every instance of new game right ? and then at the time of quitting, just accept that value from those variable and pass it to PlayerPref.
Tha would be a proper approach ? @Eric5h5 Thanks for reply
Answer by idurvesh · Nov 12, 2014 at 06:27 PM
Your score pref reset back to 0 because you are Setting Player pref to 0 everytime in Awake method of "Scores" class, its unnecessary, remove that and you code should work fine
In AddScore method its passing "score" integer as it is to player pref which is available in your default Scores class , you should do something like this,
public void AddScore(int myScore)
{
score = score + 1;
PlayerPrefs.SetInt("score", myScore);
Debug.Log(score);
}
And in OnCollision of The Collision class call ,
scores.AddScore(yourScore);
Your answer
Follow this Question
Related Questions
keep adding to playerprefs 1 Answer
Guide in ArrayPrefs or PlayerPrefx? 1 Answer
PlayerPrefs Highscore problem 1 Answer
calculate playerprefs and display 1 Answer
Highscoring and changing scripts (Java/UnityScript) 1 Answer