Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make Start() event functions of a scene run after a function of the previous scene

Discussion in 'Scripting' started by guirx, Jan 6, 2021.

  1. guirx

    guirx

    Joined:
    Sep 5, 2020
    Posts:
    19
    Hi everyone,

    I have my main scene, where there is a play button that trigger a new scene. That button has an onClick event that call a function (who retrieve data from firestore asychronously).
    The Start event function of my new scene need that data to work. The problem is that the Start function run before my OnClick function finished and so doesn't work. I cannot change the asynchronous behavior of my firestore function (the firestore sdk only use async functions queries). So I am looking for a way to run me start functions only when my OnClick function finished.

    Is there a way to handle this ?
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I don't think you can reliably control the execution order of this (this Start() and then that Start(), could be mistaken though), what you can do is have a manager that knows the order and calls them in said order. (just use a different name for the function so unity won't call it on the first enabled pass)

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Kurt-Dekker likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,446
    Well, if you need the data of the web request be fore you can continue with your game, you should delay your loadlevel call to the point when the web request has finished. You have provided zero code, however you either have a coroutine to wait for the "firestore" request or at least a callback you can use for that. How does your OnClick method currently look like?
     
  4. guirx

    guirx

    Joined:
    Sep 5, 2020
    Posts:
    19
    Here is My onClick method, I create an instance of my database, and check if the user exists, if not I authenticate him and the upload the data(the User.GetUser line). I don't any coroutine or callback. I don't see how I can use one like it's two scripts from two different scene
    Code (CSharp):
    1. public void OnClickPlay()
    2.     {
    3.         db = FirebaseFirestore.DefaultInstance;
    4.        
    5.         CollectionReference users = db.Collection("users");
    6.  
    7.         string uid;
    8.  
    9.         if(currentUser == null)
    10.         {
    11.             Authentication.AnonymousAuth();
    12.             currentUser = User.SetUser();
    13.         }
    14.         uid = currentUser.UserId;
    15.         User.GetUser(users, uid);
    16.        
    17.         SceneManager.LoadScene("MapLevel");
    18.     }
     
  5. guirx

    guirx

    Joined:
    Sep 5, 2020
    Posts:
    19
    Thanks for the link really helpful!
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I would load the main scene, then after complete I'd additive load the 2nd scene. This guarantees the main scene is completely set up before anything happens in the 2nd scene. (if I understand the question correctly)

    You could also move the things in Start you want to delay over to Update, which should never run before Start.
     
  7. guirx

    guirx

    Joined:
    Sep 5, 2020
    Posts:
    19
    Yep you understood well.In my code the line 15 call the method GetUser that has this async method inside.

    Code (CSharp):
    1. queryUsers.GetSnapshotAsync().ContinueWithOnMainThread((querySnapshotTask)  =>
    2.             {
    3.                // Assign data to variable in order to use them in next scene
    4.             }
    5.         );
    So I have to find a way to wait that this method finish to run before go to the Scene Load in my onClick method
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You could just set 1 bool saying you want to load another scene, set another bool in that method saying you are ready to load that other scene, and in Update you just check if both bools are true. If so, you load that other scene.
     
    guirx likes this.
  9. guirx

    guirx

    Joined:
    Sep 5, 2020
    Posts:
    19
    Works perfectly thanks! (sorry for the late answer I didn't had time to try your solution sooner)
     
    Joe-Censored likes this.