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

C# GameManager spawning a blank object.

Discussion in 'Scripting' started by traderain, Jul 13, 2015.

  1. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Hi, I have a script which is my gamemanager. It works fine but for some reason it keeps spawning a blank object.
    Here is my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManger : MonoBehaviour
    5. {
    6.    
    7.         public static GameManger control;
    8.  
    9.  
    10.         public string Quest;
    11.         public double time;
    12.  
    13.         public int player_money;
    14.         public int Player_damage;
    15.         public int player_xp;
    16.         public int xp_req;
    17.         public int player_armor;
    18.         public int player_health;
    19.         public int player_potions;
    20.         public int player_armorpen;
    21.         public int player_level;
    22.         public float player_xp_modifier;
    23.         public int boosterpacks;
    24.         public string player_name;
    25.         public bool dead;
    26.  
    27.         //Enemy stats
    28.         public string monster_name;
    29.         public int monster_xp_reward;
    30.         public int monster_gold_reward;
    31.         public int monster_damage;
    32.         public int monster_armor;
    33.         public int monster_armorpen;
    34.         public int monster_health;
    35.         public bool monster_dead;
    36.  
    37.  
    38.         void Awake ()
    39.         {
    40.         this.tag = "Manger";
    41.                 if (control == null) {
    42.                         DontDestroyOnLoad (this);
    43.                         control = this;
    44.                 } else if (control != this) {
    45.                         Destroy (this);
    46.                 }
    47.         }
    48.  
    49. }
    50.  
     
  2. phocker

    phocker

    Joined:
    Sep 12, 2010
    Posts:
    57
  3. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Thanks but how should i implement my variables into it? I don't really understand what's going on.
     
  4. phocker

    phocker

    Joined:
    Sep 12, 2010
    Posts:
    57
    All of your variables are declared public, so in your code you would do this

    GameManager.Instance.player_money = 100;
    GameManager.Instance.Quest = "My Cool Quest";

    I would suggest that you wrap your variables in a getter/setter

    public int PlayerMoney { get; set; }

    Either way, it should work.
     
  5. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    I might be doing something wrong but this doesn't work.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoSingleton<GameManager>
    5. {
    6.    
    7.     protected static T instance;
    8.     public string Quest;
    9.     public double time;
    10.    
    11.     public int player_money{get;set;}
    12.     public int Player_damage{get;set;}
    13.     public int player_xp{get;set;}
    14.     public int xp_req{get;set;}
    15.     public int player_armor{get;set;}
    16.     public int player_health{get;set;}
    17.     public int player_potions{get;set;}
    18.     public int player_armorpen{get;set;}
    19.     public int player_level{get;set;}
    20.     public float player_xp_modifier{get;set;}
    21.     public int boosterpacks{get;set;}
    22.     public string player_name{get;set;}
    23.     public bool dead{get;set;}
    24.    
    25.     //Enemy stats
    26.     public string monster_name{get;set;}
    27.     public int monster_xp_reward{get;set;}
    28.     public int monster_gold_reward{get;set;}
    29.     public int monster_damage{get;set;}
    30.     public int monster_armor{get;set;}
    31.     public int monster_armorpen{get;set;}
    32.     public int monster_health{get;set;}
    33.     public bool monster_dead{get;set;}
    34.  
    35.  
    36.     public static T Instance
    37.     {
    38.         get
    39.         {
    40.             if(instance == null)
    41.             {
    42.                 instance = (T) FindObjectOfType(typeof(T));
    43.                
    44.                 if (instance == null)
    45.                 {
    46.                     Debug.LogError("An instance of " + typeof(T) +
    47.                                    " is needed in the scene, but there is none.");
    48.                 }
    49.             }
    50.            
    51.             return instance;
    52.         }
    53.     }
    54.  
    55. }
    56.  
     
  6. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Why not just create an instance of your GameManager class and access it whenever you need to :
    Code (CSharp):
    1. public static GameManager instance;
    2.  
    3. void Awake(){
    4. //create an instance
    5. instance = this;
    6. }
    7.  
    8. //Access it from another script's Start method.
    9. void Start(){
    10. GameManager.instance.Quest = "Find a gold chest";
    11. }
    12.  
     
  7. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    So leave it as it is and use instance?
     
  8. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    In my opinion, yeah.
     
  9. phocker

    phocker

    Joined:
    Sep 12, 2010
    Posts:
    57
    You don't create the class like that. There is no need to add the same boilerplate code for the instance

    Just create it and add your public vars and you are good to go. Just make sure you include instance in all of your references from other classes.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class GameManager : MonoSingleton<GameManager>
    4. {
    5.  
    6.     public string Quest;
    7.     public double time;
    8.  
    9.     public int player_money{get;set;}
    10.     public int Player_damage{get;set;}
    11.     public int player_xp{get;set;}
    12.     public int xp_req{get;set;}
    13.     public int player_armor{get;set;}
    14.     public int player_health{get;set;}
    15.     public int player_potions{get;set;}
    16.     public int player_armorpen{get;set;}
    17.     public int player_level{get;set;}
    18.     public float player_xp_modifier{get;set;}
    19.     public int boosterpacks{get;set;}
    20.     public string player_name{get;set;}
    21.     public bool dead{get;set;}
    22.  
    23.     //Enemy stats
    24.     public string monster_name{get;set;}
    25.     public int monster_xp_reward{get;set;}
    26.     public int monster_gold_reward{get;set;}
    27.     public int monster_damage{get;set;}
    28.     public int monster_armor{get;set;}
    29.     public int monster_armorpen{get;set;}
    30.     public int monster_health{get;set;}
    31.     public bool monster_dead{get;set;}
    32. }
     
  10. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    • @phocker When using your technique the gamemanager loses the stats after scene loads. Spawns a "deleted gameobject" in the editor. I reference like this:
      Code (CSharp):
      1. GameManager.Instance.player_money = 0;
      But it loses all the stats.

    • Should I use Update() in a singleton or how should I serialize it?
    • So if i am correct i should use GameManager.Instance.player_money += 10;

    "Live long and prosper"
     
    Last edited: Jul 15, 2015
  11. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Okay i finnaly solved it. It works. May not be the best. if you @phocker know a better way tell me.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour
    5. {
    6.     private static GameManager _instance;
    7.  
    8.     public string Quest;
    9.     public double time;
    10.    
    11.     public int player_money;
    12.     public int Player_damage;
    13.     public int player_xp;
    14.     public int xp_req;
    15.     public int player_armor;
    16.     public int player_health;
    17.     public int player_potions;
    18.     public int player_armorpen;
    19.     public int player_level;
    20.     public float player_xp_modifier;
    21.     public int boosterpacks;
    22.     public string player_name;
    23.     public bool dead;
    24.    
    25.     //Enemy stats
    26.     public string monster_name;
    27.     public int monster_xp_reward;
    28.     public int monster_gold_reward;
    29.     public int monster_damage;
    30.     public int monster_armor;
    31.     public int monster_armorpen;
    32.     public int monster_health;
    33.     public bool monster_dead;
    34.    
    35.     public static GameManager instance
    36.     {
    37.         get
    38.         {
    39.             if(_instance == null)
    40.             {
    41.                 _instance = GameObject.FindObjectOfType<GameManager>();
    42.                
    43.                 //Tell unity not to destroy this object when loading a new scene!
    44.                 DontDestroyOnLoad(_instance.gameObject);
    45.             }
    46.            
    47.             return _instance;
    48.         }
    49.     }
    50.    
    51.     void Awake()
    52.     {
    53.         if(_instance == null)
    54.         {
    55.             //If I am the first instance, make me the Singleton
    56.             _instance = this;
    57.             DontDestroyOnLoad(this);
    58.         }
    59.         else
    60.         {
    61.             //If a Singleton already exists and you find
    62.             //another reference in scene, destroy it!
    63.             if(this != _instance)
    64.                 Destroy(this.gameObject);
    65.         }
    66.     }
    67.    
    68.     public void Play()
    69.     {
    70.         //Play some audio!
    71.     }
    72. }
    And in the other codes I do:
    Code (CSharp):
    1. public GameManager manager;
    2.  
    3. void Start()
    4. {
    5. manager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>();
    6. }
    7.  
    8. //So i can
    9.  
    10. manager.player_money += 10;