WWW.progress failing with "Object reference not set to an instance of an object" err

Hello,

I’m able to download the image properly, but when trying to obtain the progress while it loads I’m running into an Object reference issue, and I can’t quite figure it out. Any ideas? ( Object reference not set to an instance of an object )

It happens on this line: var progress : float = wwwimg.progress;

function Update () {

 

    if (!imgStarted)                                                             // If the image hasn't started downloading...

    {

        var wwwimg : WWW = new WWW (urlimg);                                    // Download the image from urlimg

        imgStarted = true;                                                      // Set to true so it doesn't keep looping

    } 

 

    if (!imgLoaded)                                                             // if the image isn't loaded yet

    {

        var progress : float = wwwimg.progress;                                 // set a variable to hold the progress

        if (wwwimg.progress == 1)                                               // if the progress = 1 (done)

        {

            print ("it's done!");                                               // Print that it's done in consol

            var imgLoaded = true;                                               // Toggle imgLoaded to true

        }

        else

        {

            print( "Download Progress: " + 100*wwwimg.progress + "% ..." );     // If not done, print progress to consol

        }

        if (imgLoaded == true)                                                  // If image is done

        {

            renderer.material.mainTexture = wwwimg.texture;                     // Add the image as texture to this.material

        } 

    } 

}

 

 

 

 

// Below is a similar, simpler method of doing the same thing, same error, however.

 

function Update () {

 

    if (!imgStarted)                                                             // If the image hasn't started downloading...

    {

        var wwwimg : WWW = new WWW (urlimg);                                    // Download the image from urlimg

        imgStarted = true;                                                      // Set to true so it doesn't keep looping

    } 

 

    if (wwwimg.progress != 1)

    {

    print( "Download Progress: " + 100*wwwimg.progress + "% ..." );

    }

    else

    {

        if (imgLoaded == false)                                                 // If image is done

        {

            renderer.material.mainTexture = wwwimg.texture;                     // Add the image as texture to this.material

            imgLoaded = true;

        } 

    }

}

I don’t really do Javascript, but if this were C# I’d say your problem was that the variable [wwwimg] is declared inside the if statement [if (!imgStarted) ], and so does not exist outside it, while you really need it to be declared at the script level.

Maybe it already is? If so it’s being overridden in the if statement at the very start.

Here is a C# example of a functional (but wrong) way to do it base on your second example.

using UnityEngine;
using System.Collections;

public class LoadImg : MonoBehaviour {
	
	private WWW wwwimg;
	private bool imgStarted=false, imgLoaded=false;
	public string urlimg="";
	
	void Update(){
		if (!imgStarted){       
		    wwwimg = new WWW (urlimg);   
		    imgStarted = true;    
		} 
	
		if (wwwimg!=null && wwwimg.progress != 1){
			print( "Download Progress: " + 100*wwwimg.progress + "% ..." );
		}
		
		else if (imgLoaded == false){         
		    renderer.material.mainTexture = wwwimg.texture;  
		    imgLoaded = true;
		}
	}
}

And Here is an example of a better way to do it, using a CoRoutine.

using UnityEngine;
using System.Collections;

public class LoadImg : MonoBehaviour {
	
	public string urlimg="";
	
	void Start(){
		StartCoroutine(Load(urlimg));
	}
	
	void Update(){
	}
	
	private IEnumerator Load(string url)
	{
		WWW wwwimg = new WWW (url);                             
		while (wwwimg.progress != 1){
			print( "Download Progress: " + 100*wwwimg.progress + "% ..." );
			yield return new WaitForSeconds(.1f);
		}
		renderer.material.mainTexture = wwwimg.texture;
	}
}

As MarkFinn suggested, I tried using a coroutine, which really just was a different way of calling the function, since everything in the function remained the same. Turns out I do not need to call coroutine, just calling the function as posted below works.

LoadImage(imgname);  // Call the function

function LoadImage (imgname : String)
{
	var urlimg = "URL TO IMAGE GOES HERE!!!!";

  	if (!imgStarted)											// If the image hasn't started downloading...
	{
	 	var wwwimg : WWW = new WWW (urlimg);					// Download the image from urlimg
		imgStarted = true;									// Set to true so it doesn't keep looping

	 	while (wwwimg.progress != 1){
        	print( "Download Progress: " + 100 * wwwimg.progress + "% ..." );
        	yield WaitForSeconds(.1);
       	}

		if (wwwimg.progress == 1)
		{
			renderer.material.mainTexture = wwwimg.texture;			// Add the image as texture to this.material
			imgLoaded = true;
			isLoading = false;
			startClock = true;
		} 	
	}

}

I believe the issue wasn’t the way I used yield, and the while statement. Now that I have the while statement with the yield for .1 second between updates on the progress, it works. I should note, the yield seems to be required, but it doesn’t have to be for the entire load time, which is what I thought from the unity docs.

Hopefully this helps other people find a way to show a progress bar when loading documents using WWW :slight_smile: