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

Resolved Can't use async UnityWebRequest [CS1061]

Discussion in 'Scripting' started by lincolncpp, Nov 2, 2022.

  1. lincolncpp

    lincolncpp

    Joined:
    Sep 25, 2021
    Posts:
    5
    I'm getting the following error when I use await request.SendWebRequest() inside a script that belongs to an Assembly Definition. I know that some assembly reference is missing but I can't find it. Without the Assembly Definition, everything works fine

    error CS1061: 'UnityWebRequestAsyncOperation' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'UnityWebRequestAsyncOperation' could be found (are you missing a using directive or an assembly reference?)

    How to reproduce:

    1. Create a default Assembly Definition, don't need to change anything
    2. Create a script with this code
      Code (CSharp):
      1. using UnityEngine;
      2. using UnityEngine.Networking;
      3.  
      4. public class ScriptAbc : MonoBehaviour{
      5.  
      6.     public async void Anything() {
      7.         UnityWebRequest request = UnityWebRequest.Get("blabla");
      8.         await request.SendWebRequest();
      9.     }
      10.  
      11. }
      12.  
    I'm using Unity 2020.3.33f1
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,196
    Is it supposed to be awaitable?
    You can yield it in a coroutine according to docs but it doesn‘t mention async/await support.
     
    lincolncpp likes this.
  3. lincolncpp

    lincolncpp

    Joined:
    Sep 25, 2021
    Posts:
    5
    There is an assembly reference that makes it awaitable, I don't know which
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
  5. lincolncpp

    lincolncpp

    Joined:
    Sep 25, 2021
    Posts:
    5
    The weird thing is that it works without the assembly definition
     
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    It ain't supported by default so you've got some other code somewhere that makes it possible like the 2 plugins I've linked.
    If you'd create a new project, it shouldn't work either. If it does I'd be more surprised.

    The code for awaiting
    UnityWebRequestAsyncOperation
    is somewhere in your project, it would need to be in its own assembly definition as well. Then anything else using it would need to reference this assembly definition. Which is why I never bother with assembly definitions. They always complicate things more than fixing anything.
     
    Kurt-Dekker and lincolncpp like this.
  7. lincolncpp

    lincolncpp

    Joined:
    Sep 25, 2021
    Posts:
    5
    You guys are right, it's not supported by default, it's an extension from a third-party package

    Problem solved, thanks!! ;)
     
  8. PoweredOnSoftware

    PoweredOnSoftware

    Joined:
    Apr 10, 2015
    Posts:
    9
  9. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    That's because that package contains the extensions to make it awaitable.

    In 2023.1 and higher you can make use of the
    Awaitable
    API.
    https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Awaitable.html

    Another option instead of awaiting is to use the
    UnityWebRequestAsyncOperation.completed
    event to handle whatever needs to be done.
    https://docs.unity3d.com/2022.3/Documentation/ScriptReference/AsyncOperation-completed.html
    That way you don't need a fire and forget
    async void
    method.
     
    Bunny83 likes this.