Search Unity

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

Discussion in 'Scripting' started by SeriouslyNot, Aug 5, 2020.

  1. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    Let's say i have this in my Start() method:

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         _dimensions = Dimensions.instance;
    4.         // TODO: Use this only on first game-run?
    5.         ConfigureCanvas();
    6.         ResetQualitySettingsToDefault();
    7.         GetBestFontSizes();
    8.         RunQualityInspector();
    9.     }
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    It will execute in order, each one waiting for the previous one to return.
     
    SeriouslyNot likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.)
     
    SeriouslyNot likes this.
  4. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    How something like that would happen? is it by code or Unity settings like "Multithreaded rendering"?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    No, you'd have to explicitly make it happen, in code. For example:
    Code (csharp):
    1.  
    2. private void Start()
    3.     {
    4.         _dimensions = Dimensions.instance;
    5.         // TODO: Use this only on first game-run?
    6. Thread t1 = new Thread( () => ConfigureCanvas());
    7. t1.Start();
    8. Thread t2 = new Thread( () => ResetQualitySettingsToDefault());
    9. t2.Start();
    10. Thread t3 = new Thread( () => GetBestFontSizes());
    11. t3.Start();
    12. Thread t4 = new Thread( () => RunQualityInspector());
    13. t4.Start();
    14.     }
    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?
     
    SeriouslyNot likes this.
  6. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    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?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    SeriouslyNot likes this.