Hello! I have been working on a 2d game and I implemented a score system for some reasons my total score is way more **For Example: My last total score was 7 and I just got a score of 2, The total score becomes 9** And if you want to see the script here it is
public class CoinCollecter : MonoBehaviour
{
public TMP_Text score_TXT;
public TMP_Text $$anonymous$$ghScore;
int score = 0;
public AudioSource audioSource;
public AudioClip coinSFX;
public TMP_Text totalScore;
// Start is called before the first frame update
void Start()
{
totalScore.text = PlayerPrefs.GetInt("totalscore").ToString();
Time.timeScale = 1f;
GetComponent().enabled = true;
score = PlayerPrefs.GetInt("score", 0);
$$anonymous$$ghScore.text = PlayerPrefs.GetInt("Score", score).ToString();
score_TXT.text = score.ToString();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
if (PlayerPrefs.GetInt("score") == 9999)
{
PlayerPrefs.SetInt("levelReached", 2);
}
if (2 > PlayerPrefs.GetInt("levelReached", 1))
{
PlayerPrefs.SetInt("levelReached", 2);
}
audioSource.PlayOneShot(coinSFX);
score = PlayerPrefs.GetInt("score", 0) + 1;
GetComponent().enabled = false;
PlayerPrefs.SetInt("score", score);
PlayerPrefs.SetInt("totalscore", PlayerPrefs.GetInt("totalscore", 0) + PlayerPrefs.GetInt("score"));
totalScore.text = PlayerPrefs.GetInt("totalscore").ToString();
score_TXT.text = score.ToString();
if (score > PlayerPrefs.GetInt("Score", 0))
{
PlayerPrefs.SetInt("Score", score);
score_TXT.text = score.ToString();
$$anonymous$$ghScore.text = score.ToString();
}
Destroy(gameObject);
}
}
}
↧
Trending Articles
More Pages to Explore .....