0

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

Maurice Bekambo
  • 325
  • 6
  • 21
  • I don't understand your new script ... why `this.gameObject`? Where is this script attached to now? And after using `GameObject main = Resources.Load("Main") as GameObject;` you still would have to instantiate it because currently it doesn't exist yet in the scene ... and you probbaly ment something like `var mainObj = Instantiate(main); mainObj.SetActive(false);` – derHugo Sep 16 '19 at 07:49
  • Ok, I just changed the script to reflect this but it still gets stuck in the global scene I made in which I placed the main game object. Might it be because the main game object is a prefab? – Maurice Bekambo Sep 16 '19 at 07:58
  • thats not how you use `RuntimeInitializeOnLoadMethodAttribute` https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html and https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute-ctor.html – yes Sep 16 '19 at 10:46

1 Answers1

1

You can create an empty "loading scene". The scene will be purely to instantiate the objects you need and will terminate after it is done. Like a main menue in games. You can put the scripts in the scene select scene.

ecco
  • 181
  • 14
  • Ok ok. Do you know how I would be able to essentially load the gameobjects without them actually appearing since I wont necessarily need them from the start and essentially be able to instantiate them when the scene they are required in is loaded? – Maurice Bekambo Sep 16 '19 at 06:50
  • Make the main GameObject inactive (`SetActive(false)`) so any children will be loaded but invisible .. however why using load at all? You could simply have it in that first scene and disable it via the inspector then you don't even need to `Instantiate` it on runtime .. also btw using `Ressources` in a built at all is strongly discouraged by the [Docs](https://learn.unity.com/tutorial/assets-resources-and-assetbundles#5c7f8528edbc2a002053b5a7) – derHugo Sep 16 '19 at 06:54
  • @derHugo I made to the script as you can see above to reflect the feedback you gave. The only problem now is that when I try to load the new scene it gets stuck for some reason and the main camera doesnt get set to inactive? Any idea why? – Maurice Bekambo Sep 16 '19 at 07:45