I know this is an older post but it in top few results of Google when I was searching for a way to auto instantiate a static GameObject in unity. not sure about older versions but this works flawlessly with 5.6.1. I needed a way to store game settings and access them from many many scenes. I also needed access to certain unity functions(like screen res, etc) that are only accessible from a MonoBehaviour so a plain static class was not an option. what I came up is basically a singleton type unity GameObject, that if you obtain a reference to the Instance, and there isn't one of these in the scene, it auto creates it. if one exists, (changed back to a scene where it was created) the duplicate self destructs so that there is only ever one of these in the game.
The reason I wanted this was because I use Unity ScriptableObjects to store data as they can be edited from the inspector both during edit and runtime and the changes during runtime do not revert (very handy if your working with the older OnGUI because you can store the rect locations in a scriptable object and watch your controls move around during runtime and the locations don't revert when you stop play mode)
this is my very basic class (removed my specific data from it)
using UnityEngine;
public class GameSettings : MonoBehaviour
{
private static GameSettings _instance;
public static GameSettings Instance
{
get
{
if (_instance != null) return _instance;
else
{
Setup();
return _instance;
}
}
}
public void Start ()
{
DontDestroyOnLoad(this);
}
public void Awake()
{
if (_instance != null) Destroy(this);
}
public static void Setup()
{
GameObject settings = GameObject.Find("GameSettings");
if (settings == null)
{
settings = new GameObject("GameSettings");
settings.AddComponent<GameSettings>();
}
_instance = settings.GetComponent<GameSettings>();
}
}
You can use this 2 ways:
1: add it to a GameObject in your very first scene (so you can modify the values from the inspector). If you ever change back to that scene it won't create a duplicate due to the check for _instance. If you do add it to a scene manually make sure that you edit the GameObject.Find string to the name of your GameObject or else it will create a new one.
2: Simply create the reference to the ClassName.Instance which will auto create the object if it doesn't exist.
Example Implementation Script for a scene:
public class TestScene : MonoBehaviour
{
private GameSettings _settings;
public void Awake()
{
_settings = GameSettings.Instance;
}
public void Start()
{
}
public void Update()
{
}
}
now all you do is add your public data (strings, int, etc) as public non static members of the GameSettings class and simply use _settings.MemberName to get access to the data.
hope this helps someone looking for a way to auto create GameObjects on the fly. This also works if you build a Prefab and have this script attached to any of its objects. Can be handy for auto creating UI Elements on the fly in specific scenes.
If you only need it in one scene and don't need it to persist across level loads (for example auto instantiating certain UI elements in certain scenes like say npc dialog etc) simply remove the DontDestroyOnLoad from the start function of the class thats attached to the prefab.