- Home /
Cannot load image from URL passed as parameter
I am currently trying to load an image using the WWW class using the following function, which works as intended when i hardcode the url i want to load the image from into the function. However when i try to pass the url as a paramater things start to get wacky.
private IEnumerator LoadImage(string url, Image image)
{
//url = url.Replace(" ", string.Empty);
Debug.Log(url);
//url = "https://news.nationalgeographic.com/content/dam/news/photos/000/755/75552.ngsversion.1422285553360.adapt.1900.1.jpg"; //random cat image
//Debug.Log(url);
Texture2D texture;
Sprite sprite;
using (WWW www = new WWW(url))
{
yield return www;
texture = www.texture;
Rect rec = new Rect(0f, 0f, www.texture.width, www.texture.height);
sprite = Sprite.Create(texture, rec, Vector2.one / 2f, 100f);
if (image != null)
{
image.sprite = sprite;
//image.color = Color.cyan;
}
}
}
using the string from the parameter url the image won't load, only giving the red question mark on the white background. However when i hardcode the URL into the function (as commented out in the code section above) the image loads just fine.
i pass the arguments from the following piece of code:
ResponseClass response = QueryResponseQueue.GetResponse(sequenceNumber); string url = response.data[1]; StartCoroutine(LoadImage(url, image));
ResponseClass contains int sequenceNumber
and string[] data
of which data[0] is the status of the query (OK for succeeded query, error otherwise, this returns OK) and data[1] is the url of the image (which works in the browser if i log it and input it into a browser).
I have tried removing any spaces that might occur at the end of the string and have logged the result from before and after i hardcode in the url. both Debug.Log(url); (before and after hardcoding) print the exact same string.
thanks in advance!
-Remy
Your answer
![](https://koobas.hobune.stream/wayback/20220612151534im_/https://answers.unity.com/themes/thub/images/avi.jpg)