• 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 Yagami93 · Oct 19, 2020 at 08:19 AM · unity 5eventssingletonmanagerdelegates

Help to understand events and delegates (Game Manager)

Hi everyone, I'm new to Unity so I have a lot to learn..

I'm trying to set up a game manager for my game, I have been following t$$anonymous$$s tutorial and understood most of it: https://hub.packtpub.com/creating-simple-gamemanager-using-unity3d/

Now, as per the tutorial, I have a game manager script that is a Singleton, and I have a scene called Intro, that has an empty gameobject with an Intro.cs script.

The issue I have is that when I run the game, the Intro script executes Awake and Start, but the ChangeState function is not either called, or passed to the delegate (not sure about the correct wording). I cannot really tell where the problem is, and I hope you guys can help me understanding it.

GameManager.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //list of game states
 public enum GameState { INTRO, MAIN_MENU }
 
 //callback to change the gameState
 public delegate void OnStateChangeHandler(); 
 
 public class GameManager
 {
     protected GameManager() {}
     static GameManager instance = null; //global variable where the gm instance is stored
     public event OnStateChangeHandler OnStateChange; //event to change state
     public GameState gameState { get; private set; } //getter for the current game state
 
     //getter for the gm instance
     public static GameManager Instance{
         get {
             if (GameManager.instance == null) { //if there is no instance, create one
                 GameManager.instance = new GameManager();
             }
 
             return GameManager.instance; //returns the instance
         }
     }
 
     //function to change the state
     public void SetGameState(GameState state) {
         t$$anonymous$$s.gameState = state;
         OnStateChange();
     }
 
     public void OnApplicationQuit() {
         GameManager.instance = null;
     }
 }

Intro.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class Intro : MonoBehaviour
 {
     GameManager gm;

     void Awake() {
         gm = GameManager.Instance;
         gm.OnStateChange += ChangeState;
         Debug.Log("Awake - current state: " + gm.gameState);
     }
 
     void Start() {
         Debug.Log("Start - current state: " + gm.gameState);
     }
 
     public void ChangeState() {
         gm.SetGameState(GameState.MAIN_MENU);
         Debug.Log("About to change state to: " + gm.gameState);
         Invoke("LoadLevel", 2f);
     }
 
     public void LoadLevel() {
         SceneManager.LoadScene(1);
     }
 
 }

Thanks for your support!

Comment
Add comment · Show 1
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 IggyZuk · Oct 19, 2020 at 09:35 AM 1
Share

2 Replies

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

Answer by T27M · Oct 19, 2020 at 09:21 AM

To start off the code you are looking in the article seems to have a bug, w$$anonymous$$ch is fixed in the Github repo https://github.com/bttfgames/SimpleGameManager/tree/master/Assets/Scripts (I'll leave that as a small challenge for you to figure out ;) ) However, your problem is that you have no calls to invoke the SetGameState method in your GameManager. If you follow the tutorial to completion t$$anonymous$$s is done in the Menus.cs script.


To explain a little further, the Intro.cs script is a listener to the OnStateChange event. In your Awake method you subscribed to the event with the following code:

  gm.OnStateChange += ChangeState;

T$$anonymous$$s doesn't invoke the ChangeState method. What you are essentially saying is when the OnStateChange event is "fired" I want to know about it and to handle that event I will call a function on my script i.e ChangeState()

Additionally, you are subscribing to the event and then also calling gm.SetGameState() if you t$$anonymous$$nk about it, in t$$anonymous$$s context, t$$anonymous$$s doesn't really make sense. The event is telling you that the game state has changed and then you are calling the GameManager to tell it to change state (there are possible cases where t$$anonymous$$s may be valid, but it's a little out of the scope of t$$anonymous$$s tutorial).

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 Yagami93 · Oct 19, 2020 at 12:47 PM

I see now, I was actually calling the ChangeState() wit$$anonymous$$n the very same function, that's why it doesn't make much sense and not$$anonymous$$ng happens.


I moved the gm.SetGameState() in the start function, like in the repo you kindly linked, and now it works as intended.

Thank you very much!

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

241 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 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 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 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 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 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 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 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 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 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 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

How to unsubscribe from a delegate that is inside of a singleton? 2 Answers

Making Multiple Switches Affect Different GameObjects 0 Answers

Casting methods, events, and/or delegates from strings / enum 0 Answers

Singletons in multiplayer games 1 Answer

EventManager with parameters 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