Unity Tip: Don’t use your first scene for global Script initialization.

I’ve noticed that a lot of projects online seem to use the first scene as a means of initializing global systems.

Why this is bad

Doing this means you are making yourself more dependent on your initialization scene.

I’ve seen a person streaming online, loading in the first scene countless times. Going from

Intro (Load a lot of stuff) -> Main Menu -> Level Select -> Your Level.

If you just want to test your scene, then this can become very tedious over time.

Making a game takes a lot of time, it is important to invest in making your iteration process quick and easy.

The solution

I’m happy to tell you there is a easy solution for this problem, using the [RuntimeInitializeOnLoadMethod] attribute.

In this case I’m going to demonstrate a way to have a prefab in your project that always loads first.

Create a prefab called ‘Main’ within your Resources folder, and attach this script to it.

Attaching the script is not mandatory, but in case you want to extend it in any way you can choose to do so.

Being able to set inspector fields can be beneficial, which is why the sample below still inherits from MonoBehaviour.