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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Do something after the web request ends WebRequest async await

Discussion in 'Scripting' started by cankirici34, Aug 6, 2022.

  1. cankirici34

    cankirici34

    Joined:
    Mar 27, 2020
    Posts:
    14
    hello, I am having a problem with a game I am working on, when the user presses a button, a web request is sent, but when the same button is pressed, another scene loads and the web request is interrupted because the scene is loaded, because there is a one-frame wait for another web request in that web request. The class in which the button is located and the classes in which the button's function is located are different, so I think I need to use async await, but when I looked at the examples on the internet, I did not fully understand how to do it. I mean, how can I make sure that the scene doesn't change until the web request is finished? can you please help?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Handle your web requests in scripts located on a GameObject that does not go away when scenes change.

    You can mark any GameObject to remain through scene changes with DontDestroyOnLoad()

    Be sure to Destroy() any GameObjects once they are no longer needed or else you will slowly accumulate them.
     
    Bunny83 likes this.
  3. cankirici34

    cankirici34

    Joined:
    Mar 27, 2020
    Posts:
    14
    yes, you are correct, but I cannot do the webrequest in that class because the project I am working on has a layout and more than one person is working. so i thought i could solve this situation with async await
     
  4. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    The SynchronizationContext that runs the async coninuations is not bound to the object lifecycle but the PlayerLoop (I think, not 100% sure as the Context gets called from the native side). So you could use async to execute stuff after an object was destroyed. Yet you will have to be very careful as "this" will be null and any attempt to access things like ".gameObject" will result in a MissingReferenceException.

    Here is a demo (notice how "this" becomes null):

    Code (CSharp):
    1. using System.Threading.Tasks;
    2. using UnityEngine;
    3.  
    4. public class AsyncAfterDestroyTest : MonoBehaviour
    5. {
    6.     public async void Start()
    7.     {
    8.         await Run();
    9.     }
    10.  
    11.     public async Task<int> Run()
    12.     {
    13.         Destroy(this.gameObject);
    14.  
    15.         int i = 0;
    16.         while(i++ < 30)
    17.         {
    18.             Debug.Log(i + ": " + this);
    19.             await Task.Delay(100);
    20.         }
    21.  
    22.         return i;
    23.     }
    24. }
    25.  
     
    Last edited: Aug 7, 2022