Hello!
With Playerprefs, I save the gold and score in the game scene and upload them to the Menu scene, and when Game Over, the Game Over panel opens and the total gold of the current game is displayed. But if he doesn’t win any gold in the game, the Game Over panel writes the gold amount of the previous game where it should be 0, and it should not add anything to the total gold in the Menu scene, but the amount of gold doubles. Another problem is that his level progresses as much as the score he earned in the game and this is shown on the Menu scene, but when I turn the game on and off completely, the score earned in the last game is shown in the score section in the Menu scene.
Problems:
1- When I earn 0 gold in the game, instead of writing 0 gold in the Game Over panel, the gold of the previous game is written and the place where I kept the total gold in my Menu scene doubles.
2- When I close the game completely and open it, the place where I keep the score in my menu scene will only show the score in the last game instead of showing the total score earned in all games.
How can I solve these problems?
I need your help!
I have 3 scripts:
PlayerControl script:
public class PlayerControl : MonoBehaviour
{
private int gold;
public Text goldText;
}
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "Gold")
{
PlayerPrefs.SetInt("Gold", gold);
PlayerPrefs.Save();
gold ++;
goldText.text="Gold: " + gold;
Destroy(collision.gameObject);
}
}
Game Manager Script:
public class GameManager : MonoBehaviour
{
public Text scoreText;
public Text goldTextGOPanel;
public int goldGOPanel;
private float score;
private float scoreTime;
{
void Start()
{
scoreTime=1;
}
void Update()
{
goldGOPanel=PlayerPrefs.GetInt("Gold");
goldTextGOPanel.text="Gold: " + goldGOPanel;
if (playerControlScript.gameOver==false)
{
scoreText.text=((int)score).ToString();
score += scoreTime * Time.deltaTime;
PlayerPrefs.SetFloat("score", score);
}
}
}
MenuControl script:
public class Menu : MonoBehaviour
{
public Text goldText;
public Text ScoreText;
public static int gold;
public float highScore;
public static int levelNumber;
public static float scoreAmount;
public int scoreGoal;
void Start()
{
//level system
PlayerPrefs.SetFloat("scoreTotal",PlayerPrefs.GetFloat("score") + scoreAmount);
scoreAmount=PlayerPrefs.GetFloat("scoreTotal");
levelNumber=1;
if (scoreAmount>=scoreGoal)
{
levelNumber++;
}
scoreGoal=levelNumber*100;
PlayerPrefs.SetInt("levelNumber", levelNumber);
ScoreText.text=((int)scoreAmount).ToString() + "/" + scoreGoal.ToString();
LevelNumberText.text=levelNumber.ToString();
//total gold amount
PlayerPrefs.SetInt("GoldTotal",PlayerPrefs.GetInt("Gold") + gold);
gold=PlayerPrefs.GetInt("GoldTotal");
goldText.text="Gold: " + gold;
}
}