Hello community. I am new to Unity and attempting to put together a game that I had previously written in C++ using some middleware that ended up being less than promissed. As a result my employers decided I would rebuild it in Unity. Unfortunately I know nothing about it. I have absorbed hours and hours of video tutorials, I have purchased SpriteManager2 and EZGUI (which are amazing, tip of the hat to Brady from Above and Beyond Software). The basic gameplay I can manage, I have that going, and it was nice and easy (relatively speaking). However I am trying to write a Game Manager that will handle changing between menu and game states, keep track of scores etc. The game is quite simple, it’s a forced runner (picture Robot Unicorn) where you have one life and just have to keep running, dodging and jumping obstacles, to get the highest score (or longest distance if you like).
I found some manager scripts at Silent Kraken (http://www.blog.silentkraken.com/2010/06/22/unity3d-manager-systems/) which are great, but I can’t get them working. Let me show you what I have at the moment.
I have a “ManagerManager” script. This is a persistant gameobject that does not get destroyed between scenes (hopefully that’s what will happen). It looks like this:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(GameManager))]
[RequireComponent(typeof(AudioManager))]
public class ManagerManager : MonoBehaviour
{
#region Members
private static GameManager m_gameManager;
public static GameManager m_game
{
get { return m_gameManager; }
}
private static AudioManager m_audioManager;
public static AudioManager m_audio
{
get { return m_audioManager; }
}
#endregion
#region Functions
// Use this for initialization
void Awake ()
{
//find the references
m_gameManager = GetComponent<GameManager>();
m_audioManager = GetComponent<AudioManager>();
//make this object persistent
DontDestroyOnLoad(this);
}
#endregion
}
It basically just holds all the other managers that I might need. In this case just a game manager (that handles game states) and an audio manager.
The game manager looks like this:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour
{
#region Members
private GameState m_curState;
public GameState m_state
{
get { return m_curState; }
}
#endregion
#region Functions
void Start()
{
SetState(typeof(MenuState));
Debug.Log("We are in the game manager start function");
}
void Update()
{
if(m_curState != null)
{
Debug.Log("The current state : " + m_curState.name);
m_curState.OnUpdate();
}
}
//changes the current game state
public void SetState(System.Type newStateType)
{
Debug.Log("In SetState()");
if (m_curState != null)
{
m_curState.OnDeactivate();
}
m_curState = GetComponentInChildren(newStateType) as GameState;
Debug.Log("The current state is: " + m_curState.name);
if (m_curState != null)
{
m_curState.OnActivate();
}
}
#endregion
}
This is the one that I’m having trouble with. It seems fine to me, but when it’s running it does some crazy things.
You will see I have debug lines in a few places, attempting to debug the script (as a c++ coder I struggle with not being able to step through code at run time). For some reason the very first line to get written is the one in SetState() not the one in Start().
This boggles me.
Then it Updates the first state (which is a menu state). Here is the menu state:
using UnityEngine;
using System.Collections;
public class MenuState : GameState
{
#region Members
float timer = 0.0f;
#endregion
#region Funtions
public override void OnActivate()
{
Application.LoadLevel("Menu");
Debug.Log("OnActivate called in MenuState");
}
public override void OnDeactivate()
{
}
public override void OnUpdate()
{
Debug.Log("Current time is: " + timer);
timer += Time.deltaTime;
if(timer >= 120)
{
ManagerManager.m_game.SetState(typeof(PlayState));
}
}
#endregion
}
this derives from an abstract state class that just has empty activate, deactivate and update functions. Basically this should just count down for 2 minutes then change the state to another one.
This doesn’t happen. The very next line in my debug console after the MenuState’s OnActivate() is the GameManager’s Start() function, which I thought should have been called first, and I have no idea how it’s being called now. Then the MenuState’s OnUpdate() is called, then somewho SetState() is called again.
In Unity there are multiple ManagerManager objects being created every update or so. I cannot decipher what is happening here.
If that wasn’t too confusing then hopefully someone will be able to give me a nudge in the right direction here.
Thank you in advance for your help.
Adam