Is it possible to call a function on Unity Program Start?

c#, unity-game-engine

Solution

RuntimeInitializeOnLoadMethodAttribute can help you :)

using UnityEngine;

class MyClass
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    static void OnBeforeSceneLoadRuntimeMethod()
    {
        Debug.Log("Before scene loaded");
    }
}

Problem

I was wondering if there was a way in Unity that when I start my program on a scene it fires a function first, I should add that I want this one function to work regardless of what scene I'm in. So a simple Start function wont cut it. Not sure if this is possible in Unity? ``` public void ProgramBegins() { //FIRES FIRST ON ANY SCENE //DO STUFF } ```

Original source