How to add coins value in already stored value of coins and update it using playerprefs.
I create two script ScoreCount which is count coins at the gameover and store coins value in Playerprefs. And another script CoinsCounter in which I only show store coins value in MainMenu by playerprefs. But the issue is I want to add coins in already stored coins in playerprefs. At gameover, the coin value show how many I earned. But in main menu I add these coins in already store coins. How it works?
The ScoreCount script is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreCount : MonoBehaviour
{
public TextMeshProUGUI scoreText;
public TextMeshProUGUI coinText;
public float scoreCount;
public float scorePreSeconds;
public float totalScore;
public float totalCoins;
bool isFinished;
// Start is called before the first frame update
void Start()
{
scoreCount = 0f;
scorePreSeconds = 1f;
totalScore = 0f;
PlayerPrefs.GetInt("TotalCoins");
coinText.text = totalCoins.ToString("0");
}
// Update is called once per frame
void Update()
{
scoreCount += scorePreSeconds * Time.deltaTime;
scoreText.text = totalScore.ToString("0");
TotalScore();
CoinsCount();
}
public void TotalScore()
{
if (!isFinished)
{
totalScore = ((scoreCount) + (killCount.KillCount)) / 2;
PlayerPrefs.GetInt("TotalScore", (int)totalScore);
}
if(playerHealth.isDead == true)
{
isFinished = true;
}
}
public void CoinsCount()
{
totalCoins = totalScore / 2;
PlayerPrefs.SetInt("TotalCoins", (int)totalCoins);
coinText.text = totalCoins.ToString("0");
}
}
And the CoinsCounter script is here:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class CoinsCounter : MonoBehaviour
{
public TextMeshProUGUI totalCoins;
public float TotalCoins;
// Start is called before the first frame update
void Start()
{
totalCoins.text = PlayerPrefs.GetInt("TotalCoins").ToString("0");
}
}