I am using a single menu for when you start a level, or pause/resume a level. The menu has Start New Game, Resume Saved Game, Resume Play, Go to Main Menu. If you click Resume Play, but a game has not been started, then you are stuck on a screen where you can move the player, but nothing else happens. To stop this, I put a bool called isGameStarted in the Game Manager. If isGameStarted = false, when you press Resume Play, it calls the SetupNewGame() method in the Game Manager. This should be a simple set up.
The problem I am having is isGameStarted variable. I set it to false by default and at start. I put a debug.log in the update function to monitor state of isGameStarted. The whole time the game is running the isGameStarted button says its false in the update function. However, when I press the Resume Play Button, it says isGameStarted is true while at the same time, the isGameStarted Debug.Log says it is false. So, the game resumes but it is unplayable because the spawners have not set up.
Now, if I close the Unity editor and reopen it, this funtion works fine the first time I play the game. I start the game, the update function says isGameStarted is false. I press the Resume Play button and it says the isGameStarted is false, it then executes the if statement which tells it to SetUpNewGame(). This is when the isGameStarted is set to True. The update function also changes to show the isGameStarted is set to True. The game plays fine. However, if I stop the game (without closinig Unity) and start the game again, I get the error above. I added onDisable and onDestroy methods to set the isGameStarted back to false when I exit the game, but that hasn’t worked. I don’t know why it is doing this.
It is not a scope issue because isGameStarted is declared at the start of the class. Also, Visual Studio shows all references to isGameStarted are pointing to the global scope variable. There are no other variables names isGameStarted.
When I check references, nothing set isGameStarted to true except the SetUpNewGame() function.
It is only when I click the ResumeGame button that the isGameStarted bool is set to true. I don’t know why it is doing this.
Here is my game manager code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//using UnityEngine.SocialPlatforms.Impl;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public string[] allCVCWords;
public string[] allCVCeWords;
public string[] allConsonantBlendsWords;
public string[] allConsonantBlendEndingWords;
public string[] allRControlledWords;
public string[] allDigraphWords;
public int sceneLevel;
public string playerPrefsGameType;
public int playerPrefsLevel = 0;
private bool isGameStarted = false;
private void Awake()
{
//Debug.Log("Game Manager Awake Function Called");
if (instance == null)
{
instance = this;
//DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
//InitializeLevel();
}
// Start is called before the first frame update
void Start()
{
//Debug.Log("Start Function Called in game Manager");
//InitializeLevel();
isGameStarted = false;
Debug.Log("from GameManager Start Function isGameStarted = " + isGameStarted);
PauseGame();
}
// Update is called once per frame
void Update()
{
Debug.Log("from GameManager Update Function isGameStarted = " + isGameStarted);
}
//public void InitializeLevel()
//{
// //Debug.Log("Starting Scene");
// //UIManager.Instance.StartNewOrResumeOld();
// PauseGame();
//}
private void OnDestroy()
{
isGameStarted = false;
}
private void OnDisable()
{
isGameStarted =false;
}
public void SetUpNewGame()
{
Debug.Log("Setting up new game");
playerPrefsLevel = 0;
PlayerPrefsManager();
Spawner.instance.SetUpWordLevelWordList(playerPrefsLevel);
Spawner.instance.SetUpListOfShortWords();
Spawner.instance.GetNewWord();
Spawner.instance.StartSpawning();
isGameStarted = true;
Time.timeScale = 1.0f;
}
public void SetUpSavedGame()
{
//SceneManager.LoadScene(sceneToLoad);
//playerPrefsLevel = PlayerPrefs.GetInt(SceneManager.GetActiveScene().name + "level");
ScoreManager.instance.score = PlayerPrefs.GetInt(SceneManager.GetActiveScene().name + "score");
UIManager.Instance.UpdateScoreUI(ScoreManager.instance.score);
Spawner.instance.SpawnerCreation();
//Spawner.instance.SetUpWordLevelWordList(playerPrefsLevel);
Spawner.instance.SetUpListOfShortWords();
Spawner.instance.GetNewWord();
Spawner.instance.StartSpawning();
//PlayerPrefsManager();
Time.timeScale = 1.0f;
}
public void GameOver()
{
ScoreManager.instance.StopScore();
UIManager.Instance.GameOver();
Time.timeScale= 0;
isGameStarted= false;
//UIManager.Instance.GetComponent<UIManager>().GameOver();
}
public void WordTracker()
{
//Debug.Log("Do level change stuff here");
Time.timeScale = 0;
playerPrefsLevel += 5;
Debug.Log("level change is calling player prefs");
//PlayerPrefsManager();
// stop spawning
// stop player
// save score and progress
// put up a congrats for completing level page
// option to start new level
}
public void LevelComplete()
{
UIManager.Instance.OpenLevelCompletePanel();
}
public void PauseGameToReviewWords(List<string> reviewWords)
{
Time.timeScale= 0.0f;
UIManager.Instance.reviewWordsPanel.SetActive(true);
UIManager.Instance.reviewWordsPanel.GetComponent<ReviewWordsPanelSetUp>().SetUpButtons(reviewWords);
}
public void PauseGame()
{
Time.timeScale = 0f;
if(SceneManager.GetActiveScene().name != "menu")
{
UIManager.Instance.PauseGameOpenPanel();
}
}
public void ResumeGame()
{
Debug.Log("from GameManager ResumeGame function isGameStarted = " + isGameStarted);
if (!isGameStarted)
{
Debug.Log("The game is not started. Going to start game now.");
SetUpNewGame();
}
UIManager.Instance.UIManagerResumeGame();
GameObject.FindObjectOfType<PlaneController>().PlayerStartPosition();
Time.timeScale = 1f;
}
public void ShowWordsCollected()
{
// set up a panel on the UI with 5 buttons, each button will pull from the dictionary, get dictionary from other games
}
public void Replay()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void GoToMainMenu()
{
SceneManager.LoadScene("menu");
}
public void QuitGame()
{
Application.Quit();
}
public void PlayerPrefsManager()
{
PlayerPrefs.SetString("gameType", SceneManager.GetActiveScene().name);
PlayerPrefs.SetInt(SceneManager.GetActiveScene().name + "level", playerPrefsLevel);
PlayerPrefs.SetInt(SceneManager.GetActiveScene().name + "score", ScoreManager.instance.score);
//Debug.Log("Player Prefs scene: " + PlayerPrefs.GetString("gameType"));
//Debug.Log("Player Prefs level: " + PlayerPrefs.GetInt(SceneManager.GetActiveScene().name));
playerPrefsGameType = PlayerPrefs.GetString("gameType");
playerPrefsLevel = PlayerPrefs.GetInt(SceneManager.GetActiveScene().name);
//Debug.Log("Player Prefs Manager completed");
}
}