C# GameManager spawning a blank object.

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:

using UnityEngine;
using System.Collections;

public class GameManger : MonoBehaviour
{
   
        public static GameManger control;


        public string Quest;
        public double time;

        public int player_money;
        public int Player_damage;
        public int player_xp;
        public int xp_req;
        public int player_armor;
        public int player_health;
        public int player_potions;
        public int player_armorpen;
        public int player_level;
        public float player_xp_modifier;
        public int boosterpacks;
        public string player_name;
        public bool dead;

        //Enemy stats
        public string monster_name;
        public int monster_xp_reward;
        public int monster_gold_reward;
        public int monster_damage;
        public int monster_armor;
        public int monster_armorpen;
        public int monster_health;
        public bool monster_dead;


        void Awake ()
        {
        this.tag = "Manger";
                if (control == null) {
                        DontDestroyOnLoad (this);
                        control = this;
                } else if (control != this) {
                        Destroy (this);
                }
        }

}

You are trying to create a Singleton - try using this instead

https://gitlab.com/spockerdotnet/sdnsingletons/blob/develop/Assets/SDNSingletons/Scripts/MonoSingleton.cs

Your class would be defined as

public class GameManager : MonoSingleton

Thanks but how should i implement my variables into it? I don’t really understand what’s going on.

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.

I might be doing something wrong but this doesn’t work.

using UnityEngine;
using System.Collections;

public class GameManager : MonoSingleton<GameManager>
{
   
    protected static T instance;
    public string Quest;
    public double time;
   
    public int player_money{get;set;}
    public int Player_damage{get;set;}
    public int player_xp{get;set;}
    public int xp_req{get;set;}
    public int player_armor{get;set;}
    public int player_health{get;set;}
    public int player_potions{get;set;}
    public int player_armorpen{get;set;}
    public int player_level{get;set;}
    public float player_xp_modifier{get;set;}
    public int boosterpacks{get;set;}
    public string player_name{get;set;}
    public bool dead{get;set;}
   
    //Enemy stats
    public string monster_name{get;set;}
    public int monster_xp_reward{get;set;}
    public int monster_gold_reward{get;set;}
    public int monster_damage{get;set;}
    public int monster_armor{get;set;}
    public int monster_armorpen{get;set;}
    public int monster_health{get;set;}
    public bool monster_dead{get;set;}


    public static T Instance
    {
        get
        {
            if(instance == null)
            {
                instance = (T) FindObjectOfType(typeof(T));
               
                if (instance == null)
                {
                    Debug.LogError("An instance of " + typeof(T) +
                                   " is needed in the scene, but there is none.");
                }
            }
           
            return instance;
        }
    }

}

Why not just create an instance of your GameManager class and access it whenever you need to :

public static GameManager instance;

void Awake(){
//create an instance
instance = this;
}

//Access it from another script's Start method.
void Start(){
GameManager.instance.Quest = "Find a gold chest";
}

So leave it as it is and use instance?

In my opinion, yeah.

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.

using UnityEngine;
using System.Collections;
public class GameManager : MonoSingleton<GameManager>
{
  
    public string Quest;
    public double time;
  
    public int player_money{get;set;}
    public int Player_damage{get;set;}
    public int player_xp{get;set;}
    public int xp_req{get;set;}
    public int player_armor{get;set;}
    public int player_health{get;set;}
    public int player_potions{get;set;}
    public int player_armorpen{get;set;}
    public int player_level{get;set;}
    public float player_xp_modifier{get;set;}
    public int boosterpacks{get;set;}
    public string player_name{get;set;}
    public bool dead{get;set;}
  
    //Enemy stats
    public string monster_name{get;set;}
    public int monster_xp_reward{get;set;}
    public int monster_gold_reward{get;set;}
    public int monster_damage{get;set;}
    public int monster_armor{get;set;}
    public int monster_armorpen{get;set;}
    public int monster_health{get;set;}
    public bool monster_dead{get;set;}
}
  • @phocker When using your technique the gamemanager loses the stats after scene loads. Spawns a “deleted gameobject” in the editor. I reference like this: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"

Okay i finnaly solved it. It works. May not be the best. if you @phocker know a better way tell me.

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{
    private static GameManager _instance;

    public string Quest;
    public double time;
   
    public int player_money;
    public int Player_damage;
    public int player_xp;
    public int xp_req;
    public int player_armor;
    public int player_health;
    public int player_potions;
    public int player_armorpen;
    public int player_level;
    public float player_xp_modifier;
    public int boosterpacks;
    public string player_name;
    public bool dead;
   
    //Enemy stats
    public string monster_name;
    public int monster_xp_reward;
    public int monster_gold_reward;
    public int monster_damage;
    public int monster_armor;
    public int monster_armorpen;
    public int monster_health;
    public bool monster_dead;
   
    public static GameManager instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = GameObject.FindObjectOfType<GameManager>();
               
                //Tell unity not to destroy this object when loading a new scene!
                DontDestroyOnLoad(_instance.gameObject);
            }
           
            return _instance;
        }
    }
   
    void Awake()
    {
        if(_instance == null)
        {
            //If I am the first instance, make me the Singleton
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            //If a Singleton already exists and you find
            //another reference in scene, destroy it!
            if(this != _instance)
                Destroy(this.gameObject);
        }
    }
   
    public void Play()
    {
        //Play some audio!
    }
}

And in the other codes I do:

public GameManager manager;

void Start()
{
manager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>();
}

//So i can

manager.player_money += 10;