Hi! Eh my first question for the unity community
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 ). 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ā¦