-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScoreSystem.cs
More file actions
36 lines (28 loc) · 926 Bytes
/
ScoreSystem.cs
File metadata and controls
36 lines (28 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ScoreSystem : MonoBehaviour
{
[SerializeField] private TMP_Text scoreText;
[SerializeField] private float scoreMultiplier;
public const string HighScoreKey = "HighScore";
public const string FinalScoreKey = "FinalScore";
private float score;
// Update is called once per frame
void Update()
{
score += Time.deltaTime * scoreMultiplier;
scoreText.text = Mathf.FloorToInt(score).ToString();
}
private void OnDestroy()
{
int finalScore = PlayerPrefs.GetInt(FinalScoreKey, 0);
int currentHighScore = PlayerPrefs.GetInt(HighScoreKey, 0);
PlayerPrefs.SetInt(FinalScoreKey, Mathf.FloorToInt(score));
if(score > currentHighScore)
{
PlayerPrefs.SetInt(HighScoreKey, Mathf.FloorToInt(score));
}
}
}