简体   繁体   中英

'UnityWebRequest' does not contain a definition for 'result'

When I use the code satable for the unity 2021 in the unity 2019.

The console shows that

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

Bugs/problem:

if (req.result == UnityWebRequest.Result.ConnectionError || req.result == UnityWebRequest.Result.ProtocolError)

I expect I can use those code on unity 2019 with other codes and works.

Simply consult the API!

result was added in version 2020.3.

Prior to that version simply follow the examples from the according version API eg 2019.4 API

You can eg simply check if there is any content in error

using (var webRequest = UnityWebRequest.Get(uri))
{
    yield return webRequest.SendWebRequest();

    if (!string.IsNullOrWhiteSpace(webRequest.error))
    {
        Debug.LogError($"Error {webRequest.responseCode} - {webRequest.error}");
        yield break;
    }

    Debug.Log(webRequest.downloadHandler.text);
}

or if you want to further differentiate isNetworkError (includes errors like no internet connection, host not reachable, DNS resolve error etc) and isHttpError (basically same as responseCode >= 400 )


If your question is about downwards compatibility but support both versions either stick to the pre-2020.3 way or use Conditional Compilation and do eg

#if UNITY_2020_3_OR_NEWER
    if(webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)  
#else
    if(!string.IsNullOrWhiteSpace(webRequest.error))
#endif
    {
        Debug.LogError($"Error {webRequest.responseCode} - {webRequest.error}");
        yield break;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM