Reputation:
How do I make the code continue to next steps if the yield return for WWW class
does not return a value (in case the internet is down)?
Currently the code is stuck at yielding value and does not go ahead.
void Start()
{
StartCoroutine(GetTimers());
}
IEnumerator GetTimers()
{
WWW data = new WWW("http://TimerWebsiteHosting.com/GetTimers.php");
yield return data;
dataString = data.text;
Items = dataString.Split(';');
Timer1 = int.Parse(Items[0]);
Timer2 = int.Parse(Items[1]);
}
Upvotes: 0
Views: 291
Reputation: 90679
First of all: The WWW
is by long obsolete!
You should rather use a UnityWebRequest
!
You can set a timeout using UnityWebRequest.timeout
And then add an error check. On success read out the data.downloadHandler.text
IEnumerator GetTimers()
{
using(UnityWebRequest www = UnityWebRequest.Get("http://TimerWebsiteHosting.com/GetTimers.php"))
{
// wait up to one second or whatever you want to use as timeout
www.timeout = 1;
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError($"Downlaod failed with {www.responseCode} - {www.error}", this);
yield break;
}
dataString = www.downloadHandler.text;
}
Items = dataString.Split(';');
Timer1 = int.Parse(Items[0]);
Timer2 = int.Parse(Items[1]);
}
Upvotes: 3