I've got a state machine set up that I'm having an issue with. I have a Start scene, a Level_01 Scene, and a GameOver Scene. Each scene has a corresponding state. When the game starts, it sets the game to the Start scene. When I click the start button, it loads the Gameplay state. The game plays fine, but when you lose, the GameOver state does not load, the game continues to run, and I get one error that says the Game Manager has been destroyed and I am still trying to reference it. I have my managers set up on a Singleton pattern and I'm not sure how it's getting destroyed. This is my first time using both the state machine and the singleton, and I also have very little experience switching between scenes so I may be doing something simple wrong. The main thing I can tell you is that the currentState variable is coming back as null when I go to hit the GameOver state, and that the code is not even getting far enough to hit the OnDeactivate for the GamePlay state (probably because currentState is null). I'm really out of ideas on this one and haven't been able to find anything useful online. Hopefully someone can show me what I'm doing wrong. Thanks!
//Managers object code:
public class Managers : MonoBehaviour
{
public static Managers instance = null;
private static GameManager _gameManager;
public static GameManager Game
{
get{ return _gameManager; }
}
private static UIManager _uiManager;
public static UIManager UI
{
get{ return _uiManager; }
}
private static LevelManager _sceneManager;
public static LevelManager Level
{
get{ return _sceneManager; }
}
private static BoardManager _boardManager;
public static BoardManager Board
{
get{ return _boardManager; }
}
private static PickUpManager _pickUpManager;
public static PickUpManager PickUp
{
get{ return _pickUpManager; }
}
private static SoundManager _soundManager;
public static SoundManager Sound
{
get { return _soundManager; }
}
void Awake ()
{
if (instance == null) {
instance = this;
} else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
_gameManager = GetComponent<GameManager>();
_uiManager = GetComponent<UIManager>();
_sceneManager = GetComponent<LevelManager>();
_boardManager = GetComponent<BoardManager>();
_pickUpManager = GetComponent<PickUpManager>();
_soundManager = GetComponent<SoundManager>();
}
}
//GamePlayState code, other states are basically the same
public class GamePlayState : _StatesBase
{
public override void OnActivate ()
{
Debug.Log("GamePlay OnActivate");
Managers.Level.LoadLevel("Level_01");
Managers.Game.isGameActive = true;
}
public override void OnDeactivate ()
{
Debug.Log("GamePlay OnDeactivate");
Managers.Game.isGameActive = false;
}
public override void OnUpdate ()
{
Debug.Log("GamePlay OnUpdate");
}
}
//GameManager code
public class GameManager : MonoBehaviour
{
public static MonoBehaviour monoBehaviour;
public bool isGameActive;
private int level = 50;
private _StatesBase currentState;
// Use this for initialization
void Awake ()
{
isGameActive = false;
InitGame();
monoBehaviour = this;
}
public _StatesBase State {
get { return currentState;}
}
void InitGame ()
{
Managers.Board.SetupScene (level);
}
public void SetState (System.Type newStateType)
{
if (currentState != null)
{
currentState.OnDeactivate ();
}
currentState = GetComponentInChildren (newStateType) as _StatesBase;
if (currentState != null)
{
currentState.OnActivate();
}
}
void Update ()
{
if (currentState != null)
{
currentState.OnUpdate();
}
}
void Start ()
{
SetState(typeof(MainMenuState));
}
}
//The load level method from the LevelManager
public void LoadLevel (string name)
{
SceneManager.LoadSceneAsync(name);
Brick.breakableCount = 0;
}

