I'm trying to implement a bootstrapper in my project that runs my game properly and allows me to play any scene in the editor without considerations. I'm also implementing a service locator. My current attempt is as follows :
namespace MindFactory
{
public class Bootstrapper : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Init()
{
Application.runInBackground = true;
RegisterService();
string bootstrapSceneName = "Bootstrap";
SceneIndex bootstrapSceneIndex = (SceneIndex)Enum.Parse(typeof(SceneIndex), bootstrapSceneName);
SceneManager.LoadScene((int)bootstrapSceneIndex, LoadSceneMode.Single);
}
static void RegisterService()
{
Service.Register(new MindFactory.Scene());
}
}
}
I learnt yesterday that BeforeSceneLoad does not have any scene information available so I am just adding the bootstrap scene via LoadSceneMode.Single. I would prefer to do a check but I don't know how. Now I'm trying to register my scene manager and I'm getting the following error
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all.
My understanding for why this occurred is anything running MonoBehavior needs to be attached to a game object. However, at BeforeSceneLoad I don't even have gameObjects loaded yet right? I'm going to be deal with audio, camera, event managers so I dont believe I can avoid MonoBehavior. I need references to audio mixers, AudioClips prefabs etc...
Any thoughts/help would be greatly appreciated. I'm totally stuck.