0

I'm getting this error when i loading my second bundle.

ArgumentException: The Object you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239) UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:200) AssetDownloader+c__Iterator0.MoveNext () (at Assets/Models/Scripts/AssetDownloader.cs:78) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

Naresh
  • 16,698
  • 6
  • 112
  • 113

2 Answers2

0
using System.Collections;
using UnityEngine;

public class ABDownloader : MonoBehaviour
{
    public static GameObject s_GearObject;
    private static bool s_CacheCleared = false;

    public string m_Url = "https://drive.google.com/uc?export=download&id=1j_iAg2ESHyNLL62CXYHb40p_X9C1oDM5";
    public string m_AssetName = "assets/prefab/h_gear.prefab";

    private void Awake()
    {
        if (!s_CacheCleared)
        {
            s_CacheCleared = true;
            Caching.ClearCache();
        }
        if (string.IsNullOrEmpty(m_Url))
        {
            Debug.LogError("[ABDownloader] Invalid URL!");
        }

        Load(m_AssetName);
    }

    private void Load(string gearName)
    {
        if (s_GearObject != null)
        {
            Destroy(s_GearObject);
        }

        StartCoroutine(LoadBundle(gearName));
    }

    private IEnumerator LoadBundle(string gearName)
    {
        while (!Caching.ready)
        {
            yield return null;
        }

        gearName = gearName.ToLowerInvariant();
        if (!gearName.StartsWith("assets/"))
        {
            Debug.LogError("[ABDownloader] Please give the asset's full path: " + gearName);
            yield break;
        }

        AssetBundle assetBundle = null;
        WWW www = WWW.LoadFromCacheOrDownload(m_Url, 0);
        yield return www;
        if (!www.isDone || !string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("[ABDownloader] Download assetBundle error: " + www.error + ", please check the URL or Network: " + m_Url);
            yield break;
        }
        assetBundle = www.assetBundle;
        AssetBundleRequest bundleRequest = assetBundle.LoadAssetAsync<GameObject>(gearName);
        yield return bundleRequest;
        if (!bundleRequest.isDone || bundleRequest.asset == null)
        {
            Debug.LogError("[ABDownloader] Load asset from AssetBundle error: " + gearName);
            yield break;
        }
        GameObject obj = bundleRequest.asset as GameObject;
        s_GearObject = Instantiate(obj) as GameObject;
        assetBundle.Unload(false);

        Debug.Log("[ABDownloader] Load asset OK!");
    }
}

Di Zhang
  • 336
  • 2
  • 7
  • Di Zhang Thank you for your Answer now I understand what's happening my script only loading my first Bundle and i want to load my 5 different bundle. Can you help me out with my problem. Here is my script I'm using: – Rishabh Rawat Feb 28 '19 at 05:34
  • @RishabhRawat Editor completed~ – Di Zhang Feb 28 '19 at 06:17
  • thank you for the scrip but now i'm having this error. [ABDownloader] Download assetBundle error: 404 Not Found, please check the URL or Network: https://drive.google.com/uc?export=download&id=1pw9gyc9fwrbg0q9d2qfm0n4nx3ozcf1s UnityEngine.Debug:LogError(Object) c__Iterator0:MoveNext() (at Assets/Models/Scripts/ABDownloader.cs:84) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) – Rishabh Rawat Feb 28 '19 at 06:36
  • @RishabhRawat URL is error, Check it, you can do this opening the URL using Chrome. – Di Zhang Feb 28 '19 at 06:42
  • The Url are Changing When I hit play. Real Url is https://drive.google.com/uc?export=download&id=1j_iAg2ESHyNLL62CXYHb40p_X9C1oDM5 but script change him to https://drive.google.com/uc?export=download&id=1j_iag2eshynll62cxyhb40p_x9c1odm5 – Rishabh Rawat Feb 28 '19 at 06:56
  • Script converting URL capital alphabets to small alphabets. Before I hit play(1j_iAg2ESHyNLL62CXYHb40p_X9C1oDM5) & After I hit play(1j_iag2eshynll62cxyhb40p_x9c1odm5) – Rishabh Rawat Feb 28 '19 at 07:05
  • @RishabhRawat My mistake, already fix the typo, please try again. – Di Zhang Feb 28 '19 at 07:38
  • Now this error is showing : https://stackoverflow.com/a/54920754/11112006 – Rishabh Rawat Feb 28 '19 at 07:48
  • IOException: Win32 IO returned ERROR_INVALID_NAME. Path: C:\Users\deeep\AppData\LocalLow\RD\AssetB Test\uc?export=download&id=1vVPA-wvot5D8tAAL61pC0WZPUDpmxv46 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor – Rishabh Rawat Feb 28 '19 at 08:22
  • (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int) System.IO.File.Create (System.String path, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:135) System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:130) System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:594) ABDownloader+c__Iterator0.MoveNext () (at Assets/Models/Scripts/ABDownloader.cs:82) – Rishabh Rawat Feb 28 '19 at 08:23
  • UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) – Rishabh Rawat Feb 28 '19 at 08:23
  • Your file name is very strenge, please modify name to a simple one like: "player.ab" – Di Zhang Feb 28 '19 at 08:57
  • do i need to add .ab on my all 5 bundles. – Rishabh Rawat Feb 28 '19 at 09:06
  • Test OK @RishabhRawat – Di Zhang Feb 28 '19 at 09:42
  • It's working but how can i load bundle with button press and it's loading only one bundle i want to load 5 different bundles with 5 different buttons. – Rishabh Rawat Feb 28 '19 at 10:32
  • Core logic: IEnumerator LoadBundle(string gearName) – Di Zhang Feb 28 '19 at 10:36
  • Di Zhang script not working.[ABDownloader] Load asset from AssetBundle error: assets/prefab/bgear.prefab UnityEngine.Debug:LogError(Object) c__Iterator0:MoveNext() (at Assets/Models/Scripts/ABDownloader.cs:70) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) – Rishabh Rawat Feb 28 '19 at 20:32
-1

public class AssetDownloader : MonoBehaviour {

//https://drive.google.com/file/d//view?usp=sharing
//16NiBSZo7kuX6UqmOE1iJHUrmXJXDGFNC
//https://drive.google.com/uc?export=download&id=16NiBSZo7kuX6UqmOE1iJHUrmXJXDGFNC

public string url;
public static GameObject Gears;
public Text loadingText;

//AssetBundle MatBundle;

//public Transform spawnPos;
public bool mClearCache = false;

void Awake()
{
    Caching.ClearCache();

    if (mClearCache)
        Caching.ClearCache();
}

void Updat()

{



}

IEnumerator LoadBundle(string GearName) 
{
    while (!Caching.ready)
    {
        yield return null;
    }

    //MATERIAL
    //WWW ww = new WWW(Mat);
    //yield return ww;

    //if (ww.error != null)
    //{
    //    throw new System.Exception("Theres as error: " + ww.error);
    //}

    //MatBundle = ww.assetBundle;
    //MatBundle.LoadAllAssets();
    //MATERIAL


    //Begin download
    WWW www = WWW.LoadFromCacheOrDownload (url, 0);
    yield return www;

    //Load the downloaded bundle
    AssetBundle bundle = www.assetBundle;

    //Load an asset from the loaded bundle
    AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(GearName, typeof(GameObject));
    yield return bundleRequest;


    //AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(GearName, typeof(GameObject));

    //get object
    GameObject obj = bundleRequest.asset as GameObject;

    // Gear = Instantiate(obj,spawnPos.position, Quaternion.identity) as GameObject;

    Gears = Instantiate(obj) as GameObject;


    loadingText.text = "";

    bundle.Unload(false);
    //www.Dispose();
}

public void Load(string GearName)
{
    if (Gears)
    {
        Destroy(Gears);
    }

    loadingText.text = "Loading...";
    StartCoroutine(LoadBundle(GearName));


}

}