Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to load prefabs/assets from a C# script in iOS

Discussion in 'Scripting' started by Citramarillo, May 19, 2014.

  1. Citramarillo

    Citramarillo

    Joined:
    May 19, 2014
    Posts:
    4
    I'm using the Resources.Load() function to load prefabs from script and everything works fine when I build and run the game in Unity, however, when I build the game for iOS no prefabs are loaded and the debug console shows:

    Code (csharp):
    1. ArgumentException: The prefab you want to instantiate is null.
    2.   at UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) [0x00000] in <filename unknown>:0
    3.   at UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) [0x00000] in <filename unknown>:0
    I've also tried using the StreamingAssets method, creating a StreamingAssets folder and using the code:

    Code (csharp):
    1. string iosPath = Application.dataPath + "/Raw/";
    2. Object blockPrefab = Resources.LoadAssetAtPath(iosPath + "blockPrefab.prefab", typeof(GameObject));
    Even if I print out the contents of the files in 'iospath', the prefabs do exist in that folder, however, nothing loads.

    Any reason why the prefab is null?

    * I'm using the free version of Unity
     
    luma2057 likes this.
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Is the prefab in the build?
    If nothing references it, it won't be included in the build, you have to put it in a resources-folder.
     
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Think of the Resources folder in the editor as being some kind of zip file when you make the game. LoadAssetAtPath needs to be given the path name of the asset as it exists in the editor, and is documented as only working in the editor. What's the code you use to load the prefab from the Resources on the iOS device? If you're getting null from Resources.Load() then something's gone wrong.
     
  4. Citramarillo

    Citramarillo

    Joined:
    May 19, 2014
    Posts:
    4
    What do you mean by including the prefab in the build? I have the prefab in the Asset folder (resource folder?). I also created folders: Asset/Raw, Asset/Resources, Asset/StreamingAssets and copied the prefabs in there as well.

    I didn't add the prefab in the GameScene hierarchy since I want to load it from script.
     
  5. Citramarillo

    Citramarillo

    Joined:
    May 19, 2014
    Posts:
    4
    This is the code I'm using:

    Code (csharp):
    1.     string iosPath = Application.dataPath + "/Raw/";
    2.     Object blockPrefab = Resources.LoadAssetAtPath(iosPath + "blockPrefab.prefab", typeof(GameObject));
    3. GameObject.Instantiate(blockPrefab, position, rotation);
     
  6. Citramarillo

    Citramarillo

    Joined:
    May 19, 2014
    Posts:
    4