Loading Textures from AssetBundle

I am currently working on a Vuforia AR Project. I am working on a part where I have an assetbundle containing png textures. There are around 177 textures. I want to download them and make them play in a sequence on a plane. This is my code for the downloading and playing part:

public class TextureLoadScript : MonoBehaviour
{
	Texture2D[] tex = new Texture2D[177];

	Material GoMaterial;

	int frameCounter = 0;

	public TrackableBehaviour trackableBehaviour;

	public string imageSequenceName="ImageSequenceName_";

	public string FolderName="Resources/ImageSequence";

	public int numOfFrames;

	AssetBundle bundle;

	bool playing;

	bool isLoading;

	void Awake()
	{
		this.GoMaterial = this.renderer.material;

	}
	// Use this for initialization
	void Start () 
	{
		if (trackableBehaviour) {
			trackableBehaviour.RegisterTrackableEventHandler (this);
		}
		tex[0] = (Texture2D)bundle.Load(FolderName+imageSequenceName+"00000",typeof(Texture2D));
	}
	// Update is called once per frame
	void Update () 
	{
		if(isLoading)
		{
			StartCoroutine("Download");
		}
		if(playing)
		{
			StartCoroutine("Play",0.0588f);
			GoMaterial.mainTexture = tex[frameCounter];
		}
	}
	IEnumerator Download()
	{

		string url = "mteam@mobilesandbox.cloudapp.net/IntroScene_android_android.unity3d";
		WWW download = WWW.LoadFromCacheOrDownload(url,1);
		yield return download;
		if(download.error!=null)
		{
			Debug.LogError(download.error);
			
		}
		bundle=download.assetBundle;
		Debug.Log ("Bundle Loaded Successfully"+bundle.name);
		isLoading = true;
	}

	IEnumerator Play(float delay)
	{
		yield return new WaitForSeconds(delay);
		 if(frameCounter<numOfFrames-1)
		{
			++frameCounter;

			tex[frameCounter] = (Texture2D)bundle.Load(FolderName+imageSequenceName+frameCounter.ToString("D5"),typeof(Texture2D));
		} 
		else
		{
			playing = false;
		}
	}

During runtime it shows object reference not set to the instance of an object. I can see I am close enough. But I am not able to proceed further. Kindly help me on this

This code has some logic errors:

  • in Start you’re accessing “bundle” but it isn’t initialized yet, since you’re initializing it in Download
  • numOfFrames is never assigned, so it’ll be ever 0
  • I think bundle.Load accepts only the name of the resource, not a path

Here is how I would do:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TextureLoadScript : MonoBehaviour {

	Material GoMaterial;
	public TrackableBehaviour trackableBehaviour;
	public string imageSequenceName="ImageSequenceName_";
	AssetBundle bundle;
	bool playing;
	bool isLoading;
	
	void Start () 
	{
		if (trackableBehaviour) {
			trackableBehaviour.RegisterTrackableEventHandler (this);
		}
		GoMaterial = renderer.material;
		isLoading = true;
		StartCoroutine(Download());
		StartCoroutine(Play(0.0588f));
	}

	IEnumerator Download()
	{
		
		string url = "mteam@mobilesandbox.cloudapp.net/IntroScene_android_android.unity3d";
		WWW download = WWW.LoadFromCacheOrDownload(url,1);
		yield return download;
		if (!string.IsNullOrEmpty(download.error))
		{
			Debug.LogError(download.error);
		}
		else
		{
			bundle = download.assetBundle;
			Debug.Log ("Bundle Loaded Successfully" + bundle.name);
		}
		isLoading = false;
	}
	
	IEnumerator Play(float delay)
	{
		while (isLoading)
			yield return null;
		playing = true;
		if (bundle != null)
		{
			List<Texture2D> textures = new List<Texture2D>((Texture2D[])bundle.LoadAll(typeof(Texture2D)));
			for (int frameCounter = 0; frameCounter < textures.Count; ++frameCounter) {
				Texture2D tex = textures.Find(t => t.name.Equals(imageSequenceName + frameCounter.ToString("D5")));
				if (tex != null)
					GoMaterial.mainTexture = tex;
				yield return new WaitForSeconds(delay);
			}
		}
		playing = false;
	}
}