I know this has been asked a bunch, but I have tried a multitude of answers to no success
I was hoping to save a high score per scene. It’s a flappy bird style game… endless scoring thing
I have 2 scenes, more eventually that I want to save the score independently per scene
This is my score saving code here (Also, I am like VERY beginner C#, like seriously, so please take that into account)
void OnPlayerDied()
{
gameOver = true;
int savedScore = PlayerPrefs.GetInt("HighScore");
if (score > savedScore)
{
PlayerPrefs.SetInt("HighScore", score);
}
SetPageState(PageState.GameOver);
}
and this is the whole GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public delegate void GameDelegate();
public static event GameDelegate OnGameStarted;
public static event GameDelegate OnGameOverConfirmed;
public static GameManager Instance;
public GameObject startPage;
public GameObject gameOverPage;
public GameObject countdownPage;
public Text scoreText;
enum PageState
{
None,
Start,
GameOver,
Countdown
}
int score = 0;
bool gameOver = true;
public bool GameOver { get { return gameOver; } }
public int Score { get { return score; } }
void Awake()
{
Instance = this;
}
void OnEnable()
{
CountdownText.OnCountdownFinished += OnCountdownFinished;
TapController.OnPlayerDied += OnPlayerDied;
TapController.OnPlayerScored += OnPlayerScored;
}
void OnDisable()
{
CountdownText.OnCountdownFinished -= OnCountdownFinished;
TapController.OnPlayerDied -= OnPlayerDied;
TapController.OnPlayerScored -= OnPlayerScored;
}
void OnCountdownFinished()
{
SetPageState(PageState.None);
OnGameStarted();
score = 0;
gameOver = false;
}
void OnPlayerDied()
{
gameOver = true;
int savedScore = PlayerPrefs.GetInt("HighScore");
if (score > savedScore)
{
PlayerPrefs.SetInt("HighScore", score);
}
SetPageState(PageState.GameOver);
}
void OnPlayerScored()
{
score++;
scoreText.text = score.ToString();
}
void SetPageState(PageState state)
{
switch (state)
{
case PageState.None:
startPage.SetActive(false);
gameOverPage.SetActive(false);
countdownPage.SetActive(false);
break;
case PageState.Start:
startPage.SetActive(true);
gameOverPage.SetActive(false);
countdownPage.SetActive(false);
break;
case PageState.GameOver:
startPage.SetActive(false);
gameOverPage.SetActive(true);
countdownPage.SetActive(false);
break;
case PageState.Countdown:
startPage.SetActive(false);
gameOverPage.SetActive(false);
countdownPage.SetActive(true);
break;
}
}
public void ConfirmGameOver()
{
//activated when replay buttron is hit
OnGameOverConfirmed(); //event sent to TapController
scoreText.text = "0";
SetPageState(PageState.Start);
}
public void StartGame()
{
//activated when play button is hit
SetPageState(PageState.Countdown);
}
}