I have the following script which I use for calculating the score
public class Score : MonoBehaviour {
Score sc;
void Start()
{
GameObject obj = GameObject.Find("ScoreSystem");
sc = obj.GetComponent();
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.tag == "Ball")
{
sc.score++;
}
} }
and this is the class where I increase the value
public class Score : MonoBehaviour {
public int score;
void Start()
{
score = 0;
}
}
But the problem I have is that the value always stays 0 when I switch to a different scene. I even used this code to store the value and display in the next scene but it didn't work
void Update()
{
PlayerPrefs.SetInt("score", score);
PlayerPrefs.Save();
}
and this code for taking the value
score = PlayerPrefs.GetInt("score");
How can I store the latest value inside an Int and display it in the next scene and when I leave that scene it reverts back to 0?
↧