Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Game manager ( mess up ) ;

Discussion in 'Editor & General Support' started by Capollastre, Apr 3, 2015.

  1. Capollastre

    Capollastre

    Joined:
    Jul 15, 2014
    Posts:
    8
    Hello folks, I am a bit new and hardly confused with the implementation of a Game Manager. After a bit research i end up with some code that i can't get it working as it is supposed to be.

    The game manager
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public enum GameState { NullState, Intro, MainMenu, Game }
    5. public delegate void OnStateChangeHandler();
    6.  
    7. public class GameManager : MonoBehaviour{
    8.     public GameState gameState { get; private set; }
    9.     public event OnStateChangeHandler OnStateChange;
    10.     static GameManager _instance;
    11.    
    12.     static public bool isActive {
    13.         get {
    14.             return _instance != null;
    15.         }
    16.     }
    17.  
    18.  
    19.     static public GameManager instance
    20.     {
    21.         get
    22.         {
    23.             if (_instance == null)
    24.             {
    25.        
    26.                 _instance = Object.FindObjectOfType(typeof(GameManager)) as GameManager;
    27.                
    28.                 if (_instance == null)
    29.                 {
    30.                     GameObject go = new GameObject("_gamemanager");
    31.  
    32.                     _instance = go.AddComponent<GameManager>();
    33.                     DontDestroyOnLoad(go);
    34.  
    35.                 }
    36.             }
    37.             return _instance;
    38.         }
    39.     }
    40.  
    41.     public void SetGameState(GameState gameState) {
    42.         this.gameState = gameState;
    43.         if(OnStateChange!=null) {
    44.             OnStateChange();
    45.         }
    46.     }
    47. }
    I suppose this its quite OK; then I first create a NEW scene Blank.... then i created a empty game object in it and called it START. ,, then i created a START script on it. that shows like this :
    Code (CSharp):
    1. sing UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. public class START : MonoBehaviour {
    7.  
    8.     GameManager GM;
    9.    
    10.     void Awake () {
    11.    
    12.         GM = GameManager.instance;
    13.         GM.OnStateChange += HandleOnStateChange;
    14.  
    15.        
    16.         Debug.Log("Current game state when Awakes: " + GM.gameState);
    17.        
    18.         GM.SetGameState(GameState.Intro);
    19.     }
    20.    
    21.     void Start () {
    22.         Debug.Log("Current game state when Starts: " + GM.gameState);
    23.    
    24.     }
    25.    
    26.     public void HandleOnStateChange ()
    27.     {
    28.         Debug.Log("Handling state change to: " + GM.gameState);
    29.         Invoke ("charge", 3f);
    30.    
    31.     }
    32.     public void charge(){
    33.         Application.LoadLevel("STAGECLEAR");
    34.     }
    35. }
    36.  
    Things seems to work fine until here.... but when i Load STAGECLEAR SCENE.... if i use same trick it throws me an error :
    MissingReferenceException: The object of type 'START' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.

    The fact is the start scene should be destroyed... and i see _gamemanager is still alive... on the new scene the point seems to broke up when i add this line on the STAGECLEAR code ( which i try to use the same thing to update de gamestate);
    STAGECLEAR CODE ( its very huge so i post the awake and start function );
    Code (CSharp):
    1. void Awake(){
    2.        
    3.  
    4.         GM = GameManager.instance;
    5.         GM.OnStateChange += HandleOnStateChange;
    6.        
    7.  
    8.     }
    9.     public void HandleOnStateChange ()
    10.     {
    11.         Debug.Log("estoyenjuego!");
    12.     }
    13.  
    14.     void Start () {
    15.         GM.SetGameState (GameState.MainMenu);
    16.        
    17.  
    18.  
    19.     }
    If i remove the line GM.SetGameState(GameState.MainMenu); from STAGECLEAR Scene... it loads the scene but the thing is that i am not able to change the gamestate inside this scene and thats the purpose of the game manager though.......

    Arghhhhh
    Hope the info is enough and thx you guys all, because im learning from 0...... and you are the teacher ! ;)
     
    hopetolive likes this.
  2. Capollastre

    Capollastre

    Joined:
    Jul 15, 2014
    Posts:
    8
    Part 1... I missplaced the post ( should be on script i Guess )
    Part 2 .. I solved myself the thing ;) with a lot more research... for those who are interested Look in Unity Resources, EVENT tutorial and DELEGATE tutorial.... its just 5m each and worth a understanding.
    Part 3. with the code above the solution might be unsubscribe the event from START script because whenever it enters to the other scene when the event is fired again it retains the function from Start which has been destroyed. so the solution could be GM.OnStateChange -= HandleOnStateChange; over OnDestroy){}.
    part 4 . Im working through on this getting a bit more complex situation where the game manager automatically do the thing depend on the gamestate and also fire the event so locally u can suscribe to the event doing different things depend who fired it;
    part 5 ... I will post the finishing result as if it helps someone to argue, help, improve or just blame me ;),

    Have a nice day ! ;)
     
    hopetolive likes this.