What order do functions run? When do they execute?

Does code in Unity run in order? In the game, my start() function has showMenu();, then below the implementation for showMenu(); is the function that gets input.

I do not understand the input function either; it’s not like std::cin >> input, the function OnUserInput(string input) just takes a parameter then outputs that parameter.

For me, OnUserInput runs before showMenu, yet the menu appears on screen without it being called inside of start().

public class HackerMode : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        showMenu();
    }

    void OnUserInput(string input)
    {
        print(input);
    }

    void showMenu()
    {
        print("Terminal started, outputting menu text.");
        Terminal.ClearScreen();
        Terminal.WriteLine("Hello.");
        Terminal.WriteLine("Welcome to the anagram hacker module\nThree difficulties exist\n");
        Terminal.WriteLine("Type 1 for easy difficulty.  \nType 2 for medium difficulty\nType 3 for hard difficulty");
    }
}

Within the above code, given that OnUserInput(); is never explicitly called, am I to expect that every function with implementation will run regardless of whether or not I call it inside of start();? How does the order of execution work? Is the class the equivalent of int main() in c++, or what?

To help illustrate what I mean, in c++,

void function1();
void function2();

int main()
{
    function1();
    function2();
}

void function1();
{
    //implementation
}

void function2()
{
    //implementation
}

Function 1 always run before function 2, and my implementation is separate from the function call; the function will not run in main unless I call it, if I commented out function1(); within main, it wouldn’t run.

Yet, in c# scripting, it seems that with the entire script, OnUserInput(string input); not only doesn’t have any call to a function that gets input, but it also executes despite not being called within Start();

Is every single function defined and implemented within a script going to run even if I never call the function?

Hi Nate,

You are right. C# scripts do have an order. The “On” in OnUserInput indicates that the method is part of an event system meaning that the method gets called when a certain event happens. Look for the method name in Ben’s and Rick’s Terminal class.

It seems that a large amount of the scripting involved in Unity is being hidden. I would like to make my own assets and frameworks for games as opposed to using other people’s. The fact that an event handler, as I later found, was searching for the function OnUserInput has confused me greatly from a programming perspective; will scripting functions like OnUserInput be done later in the course?

Event systems are not covered in this course. Students are supposed to use the scripts as they are. When you develop your own project with Unity, it is likely that you import packages or third-party assets instead of trying to reinvent the wheel. That’s why we use Unity in the first place: to save us time and trouble.

If you want to develop your own frameworks, you have to evaluate whether it makes sense to you to use the Unity framework. Maybe another engine, e.g. Godot , which is open-source, is a better engine for your purposes.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms