x


AssetBundle.LoadAsync, WWW and a for loop..

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..

more ▼

asked Sep 02 '10 at 06:38 PM

Will 10 gravatar image

Will 10
12 3 3 5

eternity is a long time.

Sep 02 '10 at 07:55 PM skovacs1

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.

Sep 02 '10 at 08:07 PM skovacs1

Does your asset 0,1.unity3d contain a Terrain?

Sep 02 '10 at 08:07 PM skovacs1

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 :)

Sep 03 '10 at 01:25 PM Will 10

If that's the case, I recommend deleting the question.

Sep 03 '10 at 02:08 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x525
x382

asked: Sep 02 '10 at 06:38 PM

Seen: 2512 times

Last Updated: Sep 02 '10 at 06:54 PM