Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmangual_83 · Jun 30, 2014 at 07:26 PM · c#

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmangual_83 · Jun 30, 2014 at 08:15 PM 0
Share

@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.

avatar image AndyMartin458 · Jun 30, 2014 at 09:04 PM 0
Share

@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.

avatar image
0

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>();
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmangual_83 · Jun 30, 2014 at 08:09 PM 0
Share

@tanoshimi Nope, still spam$$anonymous$$g the same error message :/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges