- Home /
Issues working with my game menu
So I am writing a menu for my game and I am having the following issue where the Console spams a "object not set to an instance of an object" message every time i click on the "Start Game" option. Here is what I have for my game:
public class MainMenuManager : MonoBehaviour {
public enum MenuState { MainDisplay, StartGame, Options, Extras, Exit}
public MenuState CurrentMenuState = MenuState.MainDisplay;
MainMenu_StartGame obj_StartGame;
void Awake()
{
obj_StartGame = GameObject.FindGameObjectWithTag("MainMenuItems").GetComponent<MainMenu_StartGame>();
}
public void OnStartGame()
{
CurrentMenuState = MenuState.StartGame;
}
void Update()
{
switch (CurrentMenuState)
{
case MenuState.StartGame:
obj_StartGame.ShowContent(); //NOTE: This is where I am getting the error
break;
}
}
}
public class MainMenu_StartGame : MainMenuManager
{
protected enum StartGameState { StoryMode, Trials, Return}
protected enum StoryModeState { NewGame, LoadGame, Return}
public MainMenu_StartGame(){
}
// Use this for initialization
public void ShowContent () {Debug.Log ("Start Game");}
}
Can anyone find what I am doing wrong? I Many thanks in advance!
Answer by AndyMartin458 · Jun 30, 2014 at 08:07 PM
You can probably find a way to store the reference to that game object via the inspector. If you want a quick fix, check if that object is null and try to find it.
void Update()
{
switch (CurrentMenuState)
{
case MenuState.StartGame:
if(obj_StartGame == null)
{
FindObjStartGame();
return;
}
obj_StartGame.ShowContent();
break;
}
}
void FindObjStartGame()
{
obj_StartGame = GameObject.FindGameObjectWithTag("MainMenuItems").GetComponent<MainMenu_StartGame>();
if(obj_StartGame == null)
Debug.LogError("The game object or its component is null");
}
@Andy$$anonymous$$artin458 it actually shows that my game object is null. I know now why its giving me an error and its because. The game objects have no script attached to it, at all.
Yup I figured it out. Basically what I had to do is initialize my game objects using the "new" keyword within the Start method. Thanks so much for helping me out.
@mmangual_83 glad to hear that it worked out! If you know the game object will be in the scene, another solution is to create it in the scene and add the components (scripts) to it in the inspector. You could then create a public $$anonymous$$ain$$anonymous$$enu_StartGame mmsg;
object as a part of the class and assign it via the inspector before the game ever starts. I know for some cases, the game design doesn't allow for that approach, but for most cases, the design can be modified to allow for it.
Answer by tanoshimi · Jun 30, 2014 at 08:03 PM
Try changing
void Awake()
{
obj_StartGame = GameObject.FindGameObjectWithTag("MainMenuItems").GetComponent<MainMenu_StartGame>();
}
To
void Start()
{
obj_StartGame = GameObject.FindGameObjectWithTag("MainMenuItems").GetComponent<MainMenu_StartGame>();
}
@tanoshimi Nope, still spam$$anonymous$$g the same error message :/
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
C# Making Functions Available to all Scripts 1 Answer
how to loop an animation 2 Answers