William McCarty
William McCarty

Reputation: 839

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

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
}

Upvotes: 16

Views: 29313

Answers (5)

umar hyatt
umar hyatt

Reputation: 1

//try this it work in my case
public static bool isFirstLoad;
void Awake()
{
    //your work that run only once even restart this scene
    mainPanel.SetActive(!isFirstLoad);
    if(!isFirstLoad)
    {
        isFirstLoad=true;
    }
    if (Instance == null)
    {
        Instance = this;
        DontDestroyOnLoad(this.gameObject);
    }
}

Upvotes: 0

Basic
Basic

Reputation: 26766

Yes...

Decorate your method with [RuntimeInitializeOnLoadMethod].

It will be invoked as soon as the scene has finished loading (after Awake events).

[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad() {
    Debug.Log("After Scene is loaded and game is running");
}

Documentation: https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html

Upvotes: 0

gresolio
gresolio

Reputation: 1064

RuntimeInitializeOnLoadMethodAttribute can help you :)

using UnityEngine;

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

Upvotes: 36

Kay
Kay

Reputation: 13146

I use a prefab _AppStartup which is just an empty game object having a script AppStartup. Drag this in every scene and configure AppStartup to be executed as first like @maZZZu has stated.

AppStartup performs the following jobs:

  • Global initialisation tasks when the app is started
  • Switch to boot scene if have start an arbitrary scene in editor mode
  • Scene specific initialisation tasks

    public class AppStartup : MonoBehaviour 
    {
        const int bootSceneNo = 0;
    
        public static bool veryFirstCallInApp =  true;
    
        void Awake ()
        {
            if (veryFirstCallInApp) {
                ProgramBegins ();
                if (Application.loadedLevel != bootSceneNo) {
                    // not the right scene, load boot scene and CU later
                    Application.LoadLevel (bootSceneNo);
                    // return as this scene will be destroyed now
                    return;
                } else {
                    // boot scene stuff goes here
                }
            } else {
                // stuff that must not be done in very first initialisation but afterwards
            }
            InitialiseScene ();
            veryFirstCallInApp = false;
            DestroyObject (gameObject);
        }
    
        void ProgramBegins()
        {
            // code executed only once when the app is started
        }
    
        void InitialiseScene ()
        {
            // code to initialise scene
        }
    }
    

So all you have to do is drag this prefab in every scene manually and give it -100 or whatever in the script execution order. Especially when the project grows and relies on a predefined scene flow it will save you al lot time and hassle.

Upvotes: 3

maZZZu
maZZZu

Reputation: 3615

Here you can find the execution order of all functions in unity: http://docs.unity3d.com/Manual/ExecutionOrder.html

Awake is the first function to be executed in your standalone application. For it to run you need to have a GameObject with a attached script containing the Awake function. This should be added to every scene if you want it to run regardless of the scene.

You still have to decide what is the startup scene of your game. So it is enough to add the GameObject there, if you actually want it to run just in the start of the program.

Upvotes: 3

Related Questions