Hi All,
I'm having a problem with my code, I'm trying to load nine Asset Bundles asynchronously with a nested for loop.
It works for the first iteration, then crumbles with a NullReferenceException on the second iteration.
IEnumerator TestLoad(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
string assetID = i + "," + j;
string path = "file://" + Application.dataPath + "/Resources/AssetBundles/" + assetID + ".unity3d";
print(path);
WWW download = null;
AssetBundleRequest abr = null;
Terrain terrainGameObject = null;
TerrainData terrainDataObj = null;
download = new WWW(path);
yield return download;
if (download.isDone) {
AssetBundle assetBundle = download.assetBundle;
abr = assetBundle.LoadAsync(assetID, typeof(Terrain));
yield return abr;
terrainGameObject = (Terrain)abr.asset;
}
else {
print("Problem with Download: "+assetID);
}
download.Dispose();
//Fails here with a null reference exception on second iteration
Terrain terrain = terrainGameObject;
terrainDataObj = terrain.terrainData;
}
}
}
I'm sure I'm doing something stupid, so if anyone can shed some light, I'd be eternally grateful :)
Cheers..
asked
Sep 02 '10 at 06:38 PM
Will 10
12
●
3
●
3
●
5
eternity is a long time.
You should probably throw a continue into your else statement so you don't continue when there's a download problem. You also may want to check abr.isDone before trying to access its asset.
Does your asset 0,1.unity3d contain a Terrain?
Disregard this question, it was a problem with my Asset Bundle batch creation script instead.
Figured it out this morning, many thanks for the tips though :)
If that's the case, I recommend deleting the question.