Can't Destroy Objects Instantiated from AssetBundle.LoadAll

I’ve attached an example project that tests AssetBundleLoading in a web player.

http://tagenigma.com/qa/Unity3d/AssetBundleTester/AssetBundleTester.html

The core issue can be seen from the editor.

I keep track of the order objects are instantiated so they can be destroyed in the reverse order.

This is how the objects are instantiated:

            foreach (Object obj in m_assetBundleObjects)
            {
                if (GUI.Button(new Rect(155, y += 25, 400, 20), string.Format("type: {0} name: {1}", obj.GetType(), obj.name)))
                {
                    ClearInstantiatedObjects();
                    m_instantiatedObjects.Insert(0, Object.Instantiate(obj));
                }
            }

This is how the objects are destroyed:

    void ClearInstantiatedObjects()
    {
        foreach (Object obj in m_instantiatedObjects)
        {
            Object.DestroyImmediate(obj, true);
        }
        m_instantiatedObjects.Clear();
    }

I can destroy the main asset instantiation just fine.

But I cannot destroy the objects that were instantiated from AssetBundle.LoadAll.

The errors returned are:

Can't remove component.
Can't remove Transform because MeshFilter, MeshRenderer depends on it
UnityEngine.Object:smile:estroyImmediate(Object, Boolean)
UnityEngine.Object:smile:estroyImmediate(Object, Boolean)
AssetBundleDownloader:ClearInstantiatedObjects() (at Assets\AssetBundleDownloader.cs:26)
AssetBundleDownloader:OnGUI() (at Assets\AssetBundleDownloader.cs:86)

[..\..\Runtime\Mono\MonoExportUtility.cpp line 523]

and

Can't remove component.
Can't remove Transform because MeshFilter, MeshRenderer depends on it
UnityEngine.Object:smile:estroyImmediate(Object, Boolean)
UnityEngine.Object:smile:estroyImmediate(Object, Boolean)
AssetBundleDownloader:ClearInstantiatedObjects() (at Assets\AssetBundleDownloader.cs:26)
AssetBundleDownloader:OnGUI() (at Assets\AssetBundleDownloader.cs:86)

[..\..\Runtime\Mono\MonoExportUtility.cpp line 523]

To repro:

  1. Start the project.
  2. Click download
  3. Click All Assets
  4. Click Clear Assets

Expected results:

All assets should be cleared

What happened instead:

There are assets left in the scene

140919–5151–$assetbundletester3_981.zip (1.09 MB)

I just ran into the same issue. I’m new to unity so my guess is that Object.Destroy isn’t the way to free instantiated objects, but I haven’t found any info to back that theory up.