Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

unity3d

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
}
like image 307
William McCarty Avatar asked Feb 25 '15 05:02

William McCarty


People also ask

When running a Unity project when is the start () method called?

Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.

Which function is called by Unity before game play begins?

Initialization Events It is often useful to be able to call initialization code in advance of any updates that occur during gameplay. The Start function is called before the first frame or physics update on an object.


2 Answers

RuntimeInitializeOnLoadMethodAttribute can help you :)

using UnityEngine;

class MyClass
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    static void OnBeforeSceneLoadRuntimeMethod()
    {
        Debug.Log("Before scene loaded");
    }
}
like image 82
gresolio Avatar answered Oct 12 '22 13:10

gresolio


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.

like image 21
Kay Avatar answered Oct 12 '22 13:10

Kay