I’ve got something wrong with the GameSession object persisting into the Game Over scene. The original design by Rick has the score displaying in the Game Over scene, and mine did when we first set up GameSession. It stopped displaying in video 70, “Reset Game Session”.
The score displays fine in Scene view. The GameSession object is transferred from my last level or from a game over scenario just fine and appears that it should display. My code is throwing no errors or warnings.
I have spent hours trying to find out why, and I’m hoping someone with fresh eyes might have more luck finding it. My GameSession code is below. I’ve added a few things to Rick’s design, just for practice, so hopefully that doesn’t confuse things.
Any help would be much appreciated. I’ve attached a link to my most recent build’s project files, as well as a link to the build as it was in Video 70 without all my custom edits.
Current build: Smasho v0.99
Video 70 build: Smasho v0.8
SIDE ISSUE: Also in GameSession, the AutoPlay feature worked fine transferring from one level to the next until we made a few edits late in video 79. Clicking AutoPlay On in the GameSession prefab prior to starting the game and the AutoPlay transfers from level to level fine. Any help understanding where I may have buggered it would be appreciated. So far as I can tell I made Rick’s edits to the letter.
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class GameSession : MonoBehaviour
{
// Config Parameters
[Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f; // Speed modifier for the game speed
[SerializeField] int pointsPerBlockDestroyed = 75; // Amount of points scored per block
[SerializeField] TextMeshProUGUI scoreText; // Displays the current score in the game scene
[SerializeField] TextMeshProUGUI levelText; // Displays the current level in the game scene
[SerializeField] bool isAutoPlayEnabled; // As it says
[SerializeField] bool winFlag = false; // Determines if the player sees You Win instead of Game Over
// State Variables
[SerializeField] int currentScore = 0; // As it says
int gameEndingSceneIndex = 6; // Index of the final scene
// Cached References
GameEndMessage gameEndMessage;
private void Awake()
{
int gameStatusCount = FindObjectsOfType<GameSession>().Length; // Stores how many GameStatus.cs objects there are
if(gameStatusCount > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
private void Start()
{
DisplayScore();
}
private void Update ()
{
Time.timeScale = gameSpeed;
}
public void AddToScore()
{
currentScore += pointsPerBlockDestroyed;
DisplayScore();
}
public void DisplayScore()
{
scoreText.text = currentScore.ToString();
}
public void WinTheGame()
{
winFlag = true;
}
public void ResetGame()
{
Destroy(gameObject);
}
// Below functions display the level number when the scene loads
void OnEnable()
{
SceneManager.sceneLoaded += DisplayLevel;
// Sets game ending message if the game is over
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
if (currentSceneIndex == gameEndingSceneIndex && winFlag == true)
{
gameEndMessage.DisplayGameEnding("YOU WIN!!");
}
else if (currentSceneIndex == gameEndingSceneIndex && winFlag == false)
{
gameEndMessage.DisplayGameEnding("Game Over");
}
}
void OnDisable()
{
SceneManager.sceneLoaded -= DisplayLevel;
}
public void DisplayLevel(Scene scene, LoadSceneMode mode)
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
if (currentSceneIndex != gameEndingSceneIndex)
{
levelText.text = "LEVEL " + currentSceneIndex.ToString();
Debug.Log("Displayed the level");
}
}
public bool IsAutoPlayEnabled()
{
return isAutoPlayEnabled;
}
}