We’ve recently updated our app from 2017 to 2018 and then to 2019. Somewhere along this update path a feature broke that loads textures from a web server and applies it to a game object.
Here’s the relevant part of the code:
IEnumerator LoadMaterial() {
Debug.Log("Load Material " + url);
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
Debug.Log("Sending Request " + url);
yield return www.SendWebRequest();
Debug.Log("Got Material " + url);
}
The problem is that the SendWebRequest method never returns, i.e. the third log is never printed. This won’t change if I set an explicit timeout. The target url is pointing to an S3 bucket.
Any idea why the request is never returning?
1 Like
Perhaps SendWebRequest throws exception?
Another possible case is if GameObject is destroyed of script gets disabled for other reason, then coroutine will not complete.
2 Likes
@Aurimas-Cernius I cannot see any exceptions in the logs. Also, the game object is definitely still there (not destroyed).
Try printing progress information from UnityWebRequest.
@Aurimas-Cernius :
I think I found the issue. I started the Coroutine in an OnEnabled() handler. If I do this, all yields don’t work as expected. The Coroutine just won’t be resumed. As soon as I move the Coroutine start to a Start() or Update() handler, it works.
I know that it is not possible to start a Coroutine if a game object is inactive, and you will also get an error message on the console if you do so. But I you start it from within OnEnabled(), Unity just silently stops the Coroutine on yield without ever resuming it. Since I didn’t see any error message, I assumed starting a Coroutine from OnEnabled() was ok and the problem was caused by SendWebRequest(). But actually, any yield instruction fails to resume, even yield return null.
To check this back, I looked for similar examples on the internet, and I did find blog articles where a Coroutine is started from an OnEnabled() handler, like here:
So it seemed to have worked fine at some point in time. Is this a bug then? If not, where can I find the documentation that Coroutines won’t work if called inside OnEnabled()?
My use-case is to retrieve a texture from the web, once a property on a parent game object changes, but only once the child game object gets activated. I wanted to avoid to place all these checks in an Update() loop and react on proper events instead.
3 Likes