0

Error in the script, at www.downloadHandler.data, Error: NullReferenceException: Object reference not set to an instance of an object

    private IEnumerator LoadBytes(string path, System.Action<byte[]> action) {

        UnityWebRequest www = new UnityWebRequest(path);

        yield return www.SendWebRequest();

        action(www.downloadHandler.data);

    }
derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

0

Using the constructor of UnityWebRequest creates a generic web request and you would have to set it up completely manually!

Creates a UnityWebRequest with the default options and no attached DownloadHandler or UploadHandler.

Going this way you would need to set it manually

www.downloadHanlder = new DownloadHandlerBuffer();

before starting the request.


In general what you rather want to use is UnityWebRequest.Get which already fully configures the UnityWebRequest in order to successfully perform a GET request.

This is somewhat comparable to e.g. using Socket vs TcpClient.


Then you should anyway always check if the request was successful at all.

(Also see the example from the API)

private IEnumerator LoadBytes(string path, System.Action<byte[]> action) 
{
    using(var www = UnityWebRequest.Get(path))
    {
        yield return www.SendWebRequest();

        switch (webRequest.result)
        {
            case UnityWebRequest.Result.ConnectionError:
            case UnityWebRequest.Result.DataProcessingError:
                Debug.LogError("Error: " + webRequest.error);
                break;
            case UnityWebRequest.Result.ProtocolError:
                Debug.LogError("HTTP Error: " + webRequest.error);
                break;
            case UnityWebRequest.Result.Success:
                action?.Invoke(www.downloadHandler.data);
                break;
        }
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115