Does a c# function wait for a previous function to finish for it to start?

Let’s say i have this in my Start() method:

private void Start()
    {
        _dimensions = Dimensions.instance;
        // TODO: Use this only on first game-run?
        ConfigureCanvas();
        ResetQualitySettingsToDefault();
        GetBestFontSizes();
        RunQualityInspector();
    }

Will those method calls run absolutely one after another? or they might run together at the same time?

I want to to run each function of the above 4 functions only after the previous one has finished, because it depends on the values that the previous functions produces.

It will execute in order, each one waiting for the previous one to return.

1 Like

Yes. The only way that that wouldn’t happen would be if you were running each function in a separate thread, and you have to go way out of your way to do that.

(Or starting each one with a StartCoroutine call, though even in that case you can specifically choose where you want code in each function to be able to stop and allow other code to run by where you place your “yield return” statements.)

1 Like

How something like that would happen? is it by code or Unity settings like “Multithreaded rendering”?

No, you’d have to explicitly make it happen, in code. For example:

private void Start()
    {
        _dimensions = Dimensions.instance;
        // TODO: Use this only on first game-run?
Thread t1 = new Thread( () => ConfigureCanvas());
t1.Start();
Thread t2 = new Thread( () => ResetQualitySettingsToDefault());
t2.Start();
Thread t3 = new Thread( () => GetBestFontSizes());
t3.Start();
Thread t4 = new Thread( () => RunQualityInspector());
t4.Start();
    }

So yeah, you have to try really hard to make them actually run concurrently.

If these functions go into code you don’t control there’s a tiny chance that some code inside there is kicking off something in a thread but not likely - most Unity code just doesn’t do that.

If the reason for asking this question is a particular bug you’re trying to fix, maybe be more specific about the actual bug?

1 Like

No bug, i was just curious about the original question:
if i called multiple functions after each other; will a function wait the previous one to return for it to start?

and the answer was YES. unless i was making threads like you showed me, and i’m not doing that. So i don’t have to worry about them being run simultaneously. Right?

You don’t have to worry about them running simultaneously. Unless you go out of your way to do otherwise (as already stated), all of your MonoBehaviour code runs on the “main thread”. An individual thread runs functions one at a time in order, and the main thread is no different.

1 Like