Calling an instance no mather the type

Hi everyone,

I’m not really that pro at coding so I kind of run into a small bump here.

My game has many levels and at every level, I kind of need a specific level manager. So I made this:

public class LevelsAdvancementSavers<T> : MonoBehaviour where T : Component  {

		protected static T _instance;

		/// <summary>
		/// Singleton design pattern
		/// </summary>
		/// <value>The instance.</value>
		public static T Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = FindObjectOfType<T> ();
					if (_instance == null)
					{
						GameObject obj = new GameObject ();
						_instance = obj.AddComponent<T> ();
					}
				}
				return _instance;
			}
		}

Now my problem is to call the CURRENT IN SCENE manager from the objects in the level. For example, something like this:

LevelsAdvancementSavers<T>.Instance.DoStuff ();

The problem is that this throws an error:

Assets/Customization/Scripts/LevelsManager/NextBookSection.cs(18,28): error CS0246: The type or namespace name `T’ could not be found. Are you missing an assembly reference?

I know that I could make a different script on each level to call something like this:

CurrentLevelAdvancementSaver.instance.DoStuff()

What would the correct way to reference the current type in the scene if I just want to call the current advancement saver no matter the subtype?

Thanks in advance.

you need to create a parent Manager class for all the childs. since what you are doing is just a singleton pattern and has nothing to do with polymorfism. i have created a little example of how to use singleton with polymorphism, but i would recommend not to use it. first your singleton class. You MUST have an object with your desired levelmanager in the scene and tag of GameManager

public class LevelsAdvancementSavers<T> : MonoBehaviour where T : Component
{

    protected static T _instance;

    /// <summary>
    /// Singleton design pattern
    /// </summary>
    /// <value>The instance.</value>
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<T>();
                if (_instance == null)
                {
                    _instance = GameObject.FindGameObjectWithTag("GameManager").GetComponent<T>();
                }
            }
            return _instance;
        }
    }
}

second the parent class that inherints from the singleton

public abstract class LevelManager : LevelsAdvancementSavers<LevelManager> {

    public abstract void DoStuff();
}

now you can create childs of that parent class for each scene like this

public class FirstLevelManager : LevelManager
{
    public override void DoStuff()
    {
        Debug.Log("Accessing my scene manager");
    }
}

and access the methods and instabce like this

  LevelManager.Instance.DoStuff();