AssetBundle is loaded but not shown

Hi,

I´m having some problems with AssetBundles, I can´t find where´s the error. When I run the scene, the asset bundle is downloaded and the GameObject instantiated but it doesn´t appear.

This is the code for creating the asset bundles:
CreateAssetBundles.cs

using UnityEditor;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

This is the code for loading asset bundes at run-time (of course that it´s attached to an empty object in the scene):
CatchingLoadExample.cs

using System;
using UnityEngine;
using System.Collections;

public class CachingLoadExample : MonoBehaviour
{
    private string BundleURL = "file://C:/Users/AR/Documents/UnityProjects/ASBundles/Assets/AssetBundles/test.unity3d";
    private string AssetName = "Cube";

    IEnumerator Start()
    {
        Debug.Log("Connecting");
        // Download the file from the URL. It will not be saved in the Cache
        using (WWW www = new WWW(BundleURL))
        {
            yield return www;
            if (www.error != null)
            {
                Debug.Log("Error downloading");
                throw new Exception("WWW download had an error:" + www.error);
            }

            AssetBundle bundle = www.assetBundle;

            if(bundle == null)
            {
                Debug.Log("Bundle null");
            }

            GameObject Cube;

            if (AssetName == "")
            {
                Debug.Log("Loading main asset");
                Instantiate(bundle.mainAsset);
            }
            else
            {
                Debug.Log("Loading with name");
                Cube = bundle.LoadAsset(AssetName, typeof(GameObject)) as GameObject;
                Instantiate(Cube);

                Cube.transform.Translate(new Vector3(0f, 0f, 0f));
                Cube.transform.localScale = new Vector3(1f, 1f, 1f);
                Cube.transform.localRotation = Quaternion.identity;
                Cube.SetActive(true);
            }

            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);
        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
        Debug.Log("Finish");
    }
}

When I run the scene, I can see in the console the following logs:

  • Connecting
  • Loading with name
  • Finish
    But the GameObject does´t appear.

Thanks.

PD: Sorry about my english, it´s not my first language

The solution is to run the download in a co-routine:
CatchingLoadExample.cs

using System;
using UnityEngine;
using System.Collections;

public class CachingLoadExample : MonoBehaviour
{
    private string BundleURL = "file://C:/Users/AR/Documents/UnityProjects/ASBundles/Assets/AssetBundles/test.unity3d";
    private string AssetName = "Sphere";
    private int Version = 1;
    private AssetBundle Bundle;

    IEnumerator Start()
    {
        yield return StartCoroutine(DownloadAndCache());

        if(Bundle != null)
        {
            GameObject obj = Instantiate(Bundle.LoadAsset(AssetName), Vector3.zero, Quaternion.identity) as GameObject;

            // Unload the AssetBundles compressed contents to conserve memory
            Bundle.Unload(false);
        }
    }

    private IEnumerator DownloadAndCache()
    {
        // Wait for the Caching system to be ready
        while (!Caching.ready)
        {
            yield return null;
        }

        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using (WWW www = WWW.LoadFromCacheOrDownload(BundleURL, Version))
        {
            yield return www;

            if (www.error != null)
            {
                throw new Exception("WWW download had an error:" + www.error);
            }

            Bundle = www.assetBundle;
        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }
}