NullReferenceException: Object reference not set, Help!

i have error on this script, how can i fix it.
NullReferenceException: Object reference not set to an instance of an object
LoadImage.Update () (at Assets/LoadImage.cs:25) is this line: if (!imgLink.isDone)

usingUnityEngine;
usingUnityEngine.UI;
usingSystem.Collections;

[ExecuteInEditMode]

publicclassLoadImage: MonoBehaviour
{
stringurl = "http://nfx.000webhostapp.com/gazin.jpg";
Texture2Dimg;

publicSpriteRendererspriteRenderer;
publicImageFilling;
publicGameObjectLoading;
WWWimgLink;
//Usethisforinitialization
voidStart()
{
spriteRenderer.sprite = null;
StartCoroutine(LoadImg());

}
voidUpdate ()
{
if (!imgLink.isDone)
Filling.fillAmount = imgLink.progress;
}
IEnumeratorLoadImg()
{
yieldreturn0;
//spriteRenderer.sprite = null;
imgLink = newWWW(url);
yieldreturnimgLink;

img = imgLink.texture;
Spritesprite = Sprite.Create(img, newRect(0, 0, img.width, img.height), newVector2(0.5f, 0.5f));
spriteRenderer.sprite = sprite;
Loading.SetActive (false);

}

voidOnGUI()
{
}
}

Plase use Code Tags when posting code and make sure it’s formatted correctly.

As for your problem. You call the WWW on the coroutine after a yield return 0; (Which I assume is the same as yield return null??), which means it will be called after Update in the first frame. So at the first frame the field imgLink will be null.

http://stackoverflow.com/questions/39268753/what-is-the-difference-between-yield-return-0-and-yield-return-null-in-corou

essentially yes, but at an extra cost…

1 Like

Thank you, it works great.

If you use MEC you might yield return 0. If you use Unity’s default coroutines you probably want to return null.