How can i keep my data loaded from asset bundle in the next loaded level ?

Hi there,

I created an assetbundle and i put it in the web, the downlaoding works fine, so the loading of the assets, i wrote a method ( callback) after the asset is completely doawnlaoded i sort it in an array of game objects and it works fine also .
i marked the class that hold that array of objects from bundle with dontdestroyonloading(true);
so when the new level is laoded the class remain in the game object but the data intantiated from the assetbundle.mainasset is gone missing !!
if instatiate dosen’t keep the objects how should i copy or make the objects remain in the new scene even after destroying the www.assetbundle ?

Thanks here’s some code if you want to know how i do it :

using UnityEngine;
using System.Collections;
using System;

public class CacheLoader : MonoBehaviour

public GameObject webdata;
public WebData websatascript;

public bool Finished { get; internal set; }
public   delegate void Callback(WWW www);
private string modelbundleurl = "https://blablablanlabla/Weapons.unity3d";
public int version = 1;

void Start()
    websatascript = webdata.GetComponent<WebData>();
    Downloadbundle(modelbundleurl, version, SortModels);

public void Downloadbundle(string webURL, int version , Callback callback)

    Debug.Log("start doanloading y url = " + webURL);
    StartCoroutine(LoadBundle(webURL, version, callback));
    Debug.Log("finish downloading = " + webURL);

public IEnumerator LoadBundle(string modelbundleurl, int version , Callback onDownloadFinich)
    using (WWW www = WWW.LoadFromCacheOrDownload(modelbundleurl, version))
        yield return www;
        Debug.Log("cache ready and finish for this url " + modelbundleurl);
        Finished = true;

public void SortModels(WWW www)
    Debug.Log("start sorting models method " + www);
    Vector3 pos = new Vector3(0, 0, 0);
    Quaternion rot = new Quaternion(0f, 0f, 0f, 0f);
    AssetBundle assetBundle = www.assetBundle;
    GameObject gameObject = assetBundle.mainAsset as GameObject;
      Transform[] objectsfrombundle = new Transform[60];
     Debug.Log("gameobjects from asset bundle mainasset" + gameObject.transform.childCount);
    int i = 0;
    Transform[] tab = new Transform[60];
    foreach (Transform bundleobj in gameObject.transform)
        tab *= (Transform)Instantiate(bundleobj.transform, pos, rot) as Transform;*

websatascript.objectsfrombundle = tab*.gameObject as GameObject;*
Debug.Log(“filling asset bundle in the array” + websatascript.objectsfrombundle*.name);*
Debug.Log(“length of the array” + websatascript.objectsfrombundle.Length);


It’s probably because of this line:

GameObject gameObject = assetBundle.mainAsset as GameObject;

Since your script is a Monobehaviour it already has a gameObject property.

You may also be using DontDestroyOnLoad improperly. Try using it on the gameObject that assetBundle.mainAsset gives you.

I’d recommend switching to additive scene loading and a singleton AssetBundleManager.

@Mouldi I have similar issue, have you succeeded to solve it?

my case:
I got singleton game object (Canvas) in “start scene” that has some UI images, when I switch between scenes, the references in these UI images goes to “missing sprite”.

Note: most of UI images sprites are in asset bundles.