Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

AssetBundle loads but can't retrieve the created prefab

Discussion in 'Asset Bundles' started by yohanscritch, Aug 22, 2020.

  1. yohanscritch

    yohanscritch

    Joined:
    Apr 30, 2020
    Posts:
    28
    Hi there,

    Here is the thing : I want to load Assets in my scene A, then go to my scene B and use the loaded assets there. Looks simple!

    Scene A:

    Code (CSharp):
    1. WWW request = WWW.LoadFromCacheOrDownload(url, 0);
    2.  
    3.         while(!request.isDone)
    4.         {
    5.             Debug.Log(request.progress);
    6.             yield return null;
    7.         }
    8.  
    9.         if (request.error == null)
    10.         {
    11.             ABundle = request.assetBundle;
    12.             var myAsset =  ABundle.LoadAsset("Questions");
    13.              Debug.Log(myAsset.name);
    14. }
    Here the outputs shows > Questions
    Which is great. Then in my scene B :

    Code (csharp):
    1.  
    2. var prefabQuestions = Resources.Load("Questions");
    3. GameObject go = (GameObject) Instantiate(prefabQuestions);
    4.  
    I have an error: it says prefabQuestions is null . I guess the path is wrong, but I can't find the path to the prefab I created in Scene A, what should I do ?
     
  2. Piotr_Haryza

    Piotr_Haryza

    Joined:
    Jan 4, 2019
    Posts:
    2
    From what it looks like you are trying to load the asset two times:
    1. In scene A, by doing
      Code (CSharp):
      1. Abundle.LoadAsset("Questions");
      Which loads it from the bundle
    2. In scene B by doing
      Code (CSharp):
      1. Resources.Load("Questions");
      Which loads it from resources.
    What you would like to do is either:
    1. Load this prefab both times from the bundle (as it is in a bundle) (Probably what you want)
    2. Load this prefab in scene A, and store it during scene transition, to be still loaded in scene B
    3. Try to store this loaded prefab somewhere outside of scene (but i don't think this is possibly or easy)