Using an IEnumerator to return another type of variable

Hi! Eh my first question for the unity community :slight_smile:

So!..

I wanted to make a script to load all the .jpg files in a folder into a Texture2D to use it to populate the objects in my game.

I did it with an Ienumerator type function and a coroutine (dont understand that much about them :frowning: ). Started out with a simple Ienumerator on one script, which places the texture into a global variableā€¦ But now i wanted to make this function global so i could call in every other script. So i wanted the IEnumarator function to return a Texture variableā€¦

So far i got to this:

public IEnumerator LoadImages(string f,string pathFolder, System.Action < Texture2D> result){

    text1 = new Texture2D[f.Length];
	int dummy = 0;

	foreach(string tstring in f){

		string pathImage = pathFolder + tstring;
		WWW www = new WWW(pathImage);
		yield return www;

		Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
		www.LoadImageIntoTexture(texTmp);

		text1[dummy] = texTmp ;

		dummy++;

	}
	result(text1);

}

and call it like this:

void Awake(){

	string path = @"D:\Projects\Kinect Sign\Assets\contos";

	pathPreFix = @"file://";

	subPath = System.IO.Directory.GetDirectories(path);	

	foreach(string element in subPath){
		files = System.IO.Directory.GetFiles( element, "*.jpg");
		StartCoroutine(LoadImages(files,pathPreFix,(x) => textList = x));
	}

}

If i call it in my main script it kinda worksā€¦ But on everyother script it just says: ā€œNullReferenceException: Object reference not set to an instance of an objectā€

Not that sure on whats happeningā€¦

you donā€™t want to use absolute paths because they wonā€™t exist when you deploy to whatever device. The general strategy is to create a folder called Resources in your assets folder. Then put all your images in there or create an images folder within there. Then, in your case you could call Resources.LoadAll(ā€œpath within resources I.e. Imagesā€. This method will return an object array of all the files which you can cast to 2d texture array and donā€™t even need your function