Search Unity

Discussion Official I got null reference exception when i switch between scenes I use try and catch to caught

Discussion in 'Scripting' started by sachinhello, Dec 21, 2022.

  1. sachinhello

    sachinhello

    Joined:
    Dec 27, 2018
    Posts:
    35
    I am using unity web request and make a helper class and using static method to hit on web to get data
    I am using async and await for waiting to get data and response from server in between i switch scene then i got error i am sharing my method signature to understand better which cause the problem what is the efficent way to prevent null reference logs.

    public static async Task<short> GetRequest(string url, Action<string> jsonresponseAction = null, Action<string> responseFailedAction = null, string token = null, int timeout = 0)

    error cause when using callback to get jsonresponseAction data
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    PraetorBlue and tsukimi like this.
  3. sachinhello

    sachinhello

    Joined:
    Dec 27, 2018
    Posts:
    35
    i am using this condition to avoid null reference exception in my request callback
    if(!this)
    return;
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    That sounds like you misidentified what is null.