Search code examples
c#unity-game-engineservertextureswebrequest

Error m_InstanceID != 0 when downloading texture from the server


I'm getting this error in Unity 5.4 when trying to download the texture from the server.

Here is the code (the link should work):

     UnityWebRequest www = UnityWebRequest.GetTexture("https://0.gravatar.com/avatar/fc2beef90fad49f83d79650a10b5c030?s=256&d=identicon&r=G");
     www.SetRequestHeader("Accept", "image/*");
     async = www.Send();
     while (!async.isDone)
         yield return null;
     if (www.isError) {
         Debug.Log(www.error);
     } else {
         tex = DownloadHandlerTexture.GetContent(www);    // <-------------------
     }

The error looks like this:

m_InstanceID != 0
UnityEngine.Networking.DownloadHandlerTexture:GetContent(UnityWebRequest)

Solution

  • This is a bug. It happens when www.isDone or async.isDone is used with DownloadHandlerTexture.

    The work around is to wait for another frame with yield return null; or yield return new WaitForEndOfFrame() before calling DownloadHandlerTexture.GetContent(www);.

    UnityWebRequest www = UnityWebRequest.GetTexture("https://0.gravatar.com/avatar/fc2beef90fad49f83d79650a10b5c030?s=256&d=identicon&r=G");
    www.SetRequestHeader("Accept", "image/*");
    async = www.Send();
    while (!async.isDone)
        yield return null;
    if (www.isError)
    {
        Debug.Log(www.error);
    }
    else
    {
        //yield return null; // This<-------------------
        yield return new WaitForEndOfFrame();  // OR This<-------------------
        tex = DownloadHandlerTexture.GetContent(www);   
    }
    

    Although, I don't know how reliable this is. I wouldn't use this in a commercial product unless a thorough test is performed.

    A reliable solution is to file for bug about www.isDone, then don't use www.isDone. Use yield return www.Send(); until this is fixed.

    UnityWebRequest www = UnityWebRequest.GetTexture("https://0.gravatar.com/avatar/fc2beef90fad49f83d79650a10b5c030?s=256&d=identicon&r=G");
    www.SetRequestHeader("Accept", "image/*");
    yield return www.Send(); // This<-------------------
    
    if (www.isError)
    {
        Debug.Log(www.error);
    }
    else
    {
        tex = DownloadHandlerTexture.GetContent(www);    
    }