Question by
adhamyasser · Sep 09, 2015 at 04:29 PM ·
textsavereloadcoins
when i reload the scene the score number remains the same and doesnt revert back to zero
In my game I am supposed to collect icons or coins and I also have a high score which is saved. when I collect a certain amount of coins (X) and then lose the score text still remains (X) and doesn't become zero here is my code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class iconcollect : MonoBehaviour {
public static int score = 0; // The player's score.
public Text scoretext;
public Text hiighscoretext; // Reference to the Text component.
public static iconcollect iconcolect;
public Text scoretext1;
public Text hiighscoretext1;
public int highscore;
void Start(){
}
void Awake ()
{
// Set up the reference.
scoretext = GameObject.FindGameObjectWithTag("text").GetComponent<Text>();
iconcolect = this;
highscore = PlayerPrefs.GetInt ("highscore",0);
hiighscoretext = GameObject.FindGameObjectWithTag("highscore").GetComponent<Text>();
iconcolect = this;
highscore = PlayerPrefs.GetInt ("highscore",0);
}
void Update ()
{
scoretext.text = "score:" + score ;
hiighscoretext.text = "Highscore:" + highscore;
scoretext1.text = "score:" + score ;
hiighscoretext1.text = "Highscore:" + highscore;
}
void SetCountText ()
{
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "icon")
{
score = score + 1;
Destroy(gameObject);
}
}
public void checkhighscore(){
if (score > highscore) {
PlayerPrefs.SetInt("highscore",score);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612033104im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to save money 1 Answer
when i reload the scene the score number remains the same and doesnt revert back to zero 1 Answer
Save and Load Problem, Dont quite understand. 1 Answer
Downloading files from wepage and saving on android ,Download from webpage, and open it on android 2 Answers
How can i save random-generated maps during gameplay ? 1 Answer