hi, guys, I am doing a 2d app to save and download the image I want to save the image from web and load the images save and load fine but the problem is it says coroutine cannot use thanks for listening!
error:
Coroutine ‘https://img.etimg.com/thumb/msid-67102450,width-300,imgsize-65522,resizemode-4/sedan-car-getty.jpg’ couldn’t be started!
UnityEngine.MonoBehaviour:StartCoroutine(String)
c__Iterator2:MoveNext() (at Assets/scripts/save and load/load.cs:250)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
load:seq(String) (at Assets/scripts/save and load/load.cs:245)
load:image1() (at Assets/scripts/save and load/load.cs:78)
UnityEngine.EventSystems.EventSystem:Update()
here’s the code:
void seq(string url)
{
StartCoroutine(Seq(url));
}
private IEnumerator Seq(string url)
{
yield return StartCoroutine(url);
yield return StartCoroutine(waittoload());
}
private IEnumerator imageload(string url)
{
StartCoroutine(DownloadImage(url));
yield return DownloadImage(url);
Debug.Log(“loaded”);
}
private IEnumerator waittoload()
{
yield return new WaitForSeconds(0f);
}
and the function i declared(courotine):
public void image1()
{
if (downloadimage.Instance.save == true)
{
if (downloadimage.Instance.imagesloaded <= 2)
downloadimage.Instance.loadimage1();
if (downloadimage.Instance.imagesloaded <= 2)
if (is2)
{
raw.texture = downloadimage.Instance.texture;
}
else
{
seq(downloadimage.Instance.url[0]);
is2 = true;
Debug.Log("imageloaded!");
}
}
else
{
if (downloadimage.Instance.imagesloaded <= 2)
Debug.Log("time to load");
StartCoroutine(DownloadImage(downloadimage.Instance.url[0]));
downloadimage.Instance.imagesloaded++;
}
A coroutine does not retrieve a file from the web, it runs a process on a different thread. So on line 6 of your code block it will break because you are passing the url as a parameter for which coroutine to run. It interprets that url and searches for a coroutine in your script by that name (the url as a string). What you need to look into is WWW/UnityWebRequests.
As a brief example:
// Start the coroutine
string url = "";
Startcoroutine(GetImageFromWeb(url));
private IEnumerator GetImageFromWeb(string url)
{
using (WWW request = new WWW(url))
{
yield return request;
if(request.error != null)
{
// Do something with information returned
}
else Debug.LogError("Invalid Web Request: " + www.error);
}
}
More information can be found here: Unity - Scripting API: WWW