Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AaronG · Jun 11, 2011 at 04:50 PM · c#

How do I call methods from other scripts using C#?

I'm seriously stuck with this. I have 2 scripts; a Game Manager and a UI Manager. When you hit Esc the 'gamestate' in the Game manager is set to paused and from within it will call a method from the UI manager to display a 'paused' message on the screen.

I have 2 scenes. In the first scene which is the main menu, the Game manager is initialised and continue to exist throughout every scene. In the second scene, there is a empty game object with the UI Manager attached.

When I run the game, move to level 1, and the hit escape, I get the following errors:

 NullReferenceException: Object reference not set to an instance of an object
 Game_Manager.GamePause () (at Assets/Scripts/Game_/Game_Manager.cs:55)
 Game_Manager.Update () (at Assets/Scripts/Game_/Game_Manager.cs:46)

So it's claiming that the UI Manager does not exist and so it crashes. :(

Here are the scripts...

 /// Game Manager
 
 using System.Collections;
 using UnityEngine;
 
 enum GameState { mainmenu, playing, paused, gameover };
 
 public class Game_Manager : MonoBehaviour
 {
     private static Game_Manager thisInstance;
     private static GameState mGameState;
     private UI_Manager mUI;
     
 
     void Awake()
     {
         Screen.SetResolution(800, 600, false);
 
         /// Ensures that the GameManager is persistant through all scenes
         
         if (thisInstance != null && thisInstance != this) //IF we already has an instance of this class
             Destroy(gameObject);
         else
         {
             thisInstance = this;
             DontDestroyOnLoad(this);
         }
     }
 
     void Start()
     {
         Debug.Log("Game_Manager initialised");
         mGameState = GameState.mainmenu;
     }
 
     void Update()
     {
         if (mGameState == GameState.mainmenu)
         {
             GameMainMenu();
         }
 
         /// Pausing
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             GamePause();
         }
     }
 
     public void GamePause()
     {
         if (mGameState == GameState.playing)
         {
             mUI.GamePauseGUI();
             Time.timeScale = 0.0f;
             mGameState = GameState.paused;
             Debug.Log("Game Paused");
         }
         else
         {
             Time.timeScale = 1f;
             mGameState = GameState.playing;
             Debug.Log("Game Playing");
         }
     }
 
     void GameMainMenu()
     {
         
         if (Input.anyKeyDown)
         {
             Application.LoadLevel(1);
             mGameState = GameState.playing;
         }
     }
 
     public static Game_Manager Instance
     {
         get
         {
             if (thisInstance == null)
             {
                 thisInstance = new GameObject("MySingleton").AddComponent<Game_Manager>();
             }
 
             return thisInstance;
         }
     }
 
     public void OnApplicationQuit()
     {
         thisInstance = null;
     }
 }


   

 // UI_Manager
 
 using UnityEngine;
 using System.Collections;
 
 public class UI_Manager : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         
     }
     
     void OnGUI()
     {
 
     }
 
     void Update()
     {
 
     }
 
     public void GamePauseGUI()
     {
         Debug.Log("Game Paused GUI");
     }
 }
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
1
Best Answer

Answer by Peter G · Jun 11, 2011 at 05:07 PM

My guess is that it is comming from this line:

 mUI.GamePauseGUI();

Unity does not automatically connect references except for built in accessors such as .transform. You need to manually reference the UI_Manager GameObject in order to use it. This can be done a number of ways, here's an example.

 public class Game_Manager : MonoBehaviour
 {
     private static Game_Manager thisInstance;
     private static GameState mGameState;
     //-------------------------
     private UI_Manager mUI;
     private UI_Manager MUI {
         get {
             if(mUI == null) {
                  mUI = (UI_Manager)FindObjectOfType(typeof(UI_Manager));
                   //^ this is the important line.
             }
             return mUI;
         }
     }
     //-------------------------
   
     public void GamePause() { /*use the property MUI to call your methods*/ }
 }

The important part of the above example is mUI = (UI_Manager)FindObjectOfType(typeof(UI_Manager));. The method finds an object of the given type in the scene and returns it. You need to cast it to its derived type though 95% of the time before you actually use it.

Comment
Add comment · 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
0

Answer by AaronG · Jun 11, 2011 at 05:10 PM

Thanks. Works perfectly!

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 Peter G · Jun 11, 2011 at 06:03 PM 0
Share

$$anonymous$$ake sure to mark it the correct answer then. A checkmark outline should show up under the votes for my answer that you need to click on.

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I stop a script from running from other scripts or from itself 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Why a public varibale is not accsible inside Update() ? 1 Answer


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