I'm currently making a game in Unity and I ran into a problem. Essentially what I'm currently trying to do is load a huge list of objects that I require for future scenes in my Recource folder and load them before any scene including the startup scene has been loaded so that I can essentially randomly choose to load scenes instead of being dependent on the first scene in order to load gameobjects that I will need in later scenes. I found a method online that was supposed to be able to do this. I created a gameobject called main put it into my resources folder and attached the Script bellow to it. Then I attached all the gameobjects that I would need to load before any of my scenes to this gameobject ("Main"):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Main : MonoBehaviour
{
//[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
void Start()
{
GameObject main = Instantiate(Resources.Load("Main")) as GameObject;
var mainObj = Instantiate(main);
mainObj.SetActive(false);
GameObject.DontDestroyOnLoad(main);
MySceneManager.LoadScene(1, this);
}
}
When I then run my game for somereason I get a NullReferenceexception stating: (The Object you want to instantiate is null). Does anyone know how I can solve this problem or even if their is an easier solution to the problem I'm trying to solve?
Thanks in advance