16

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 } 
4
  • would the Start() function work? Commented Feb 25, 2015 at 5:16
  • 1
    I'm looking for something so I don't have to use Start in every scene to execute the same function. So in this case, no. Commented Feb 25, 2015 at 5:17
  • 1
    Awake() is called even before start but you will have to attach it to an emty gameObject in your every scene. Commented Feb 25, 2015 at 5:30
  • 2
    not what you are looking for, but relevant: docs.unity3d.com/Manual/RunningEditorCodeOnLaunch.html Commented May 24, 2016 at 17:56

5 Answers 5

36

RuntimeInitializeOnLoadMethodAttribute can help you :)

using UnityEngine; class MyClass { [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void OnBeforeSceneLoadRuntimeMethod() { Debug.Log("Before scene loaded"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is the preferred way nowadays in most cases. But in a Hololens project using MS MixedReality Toolkit I needed more control due to some awful singletons in the framework. In this case I recommend the prefab solution from my answer
3

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.

1 Comment

I'm not sure this is what the user is asking for. The execution order is per script, not the application.
3

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.

Comments

0

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

Comments

0
//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); } } 

1 Comment

Please add further details to expand on your answer, such as working code or documentation citations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.