- Home /
count score start 0
i create game, when a ball touch a object score.text show the score=10 but i have a problem when the game is over and i start again to play the ball when touch object its start score 10 again etc i don't want it i need wen game over and i touch again a ball score continue to count where its finished like when a score is 60 and game over and i pray again score start to 70 ,
public class BallContollers : MonoBehaviour {
public int scorevalue;
Ballcontroler.cs
private GameManager gameManager;
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Diamond") {
Destroy (col.gameObject);
Destroy (part, 1f);
gameManager.AddScore (scorevalue);
}
}
gamemanager.cs
public Text scoreText;
private int Scores;
void Start () {
Scores = 0;
scoreText.text = PlayerPrefs.GetInt ("ScoreText").ToString();
PlayerPrefs.SetInt ("ScoreText", Scores);
}
public void updatescore(){
if (Scores > PlayerPrefs.GetInt ("ScoreText")) {
//IncrementScore ();
}
scoreText.text = "" + Scores;
}
public void AddScore(int newscorevalue){
Scores+= newscorevalue;
PlayerPrefs.SetInt ("ScoreText", Scores);
updatescore ();
}
}
Answer by _samuel12345 · Jun 09, 2018 at 12:35 PM
You are using Player Prefs to save the score value and then are setting score to it when you start the game. If you want the score to start at 0 every time the game starts then you have no need to use Player Prefs.
hii samuel12345 thanks but i don't wan't score start to 0 when i play again i want score to continue again and again i don' t know whats wrong with code
Answer by fluxhackspro · Jun 09, 2018 at 09:00 PM
Well in the start method you set the "Score" to 0 and then already save it as the new playerPrefs Score, thats why it's always the same. Then when you update the score you have to set it again in PlayerPrefs
Under IncrementScore(); you have to set the new score to the playerprefs:
if (Scores > PlayerPrefs.GetInt ("ScoreText")) {
//IncrementScore ();
PlayerPrefs.SetInt ("ScoreText", Scores);
}