Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/theme.php on line 623
michaeljbowen.com » Instantiating a prefab from the library in C# with Unity 3D
Home > C#, Unity 3D > Instantiating a prefab from the library in C# with Unity 3D

Instantiating a prefab from the library in C# with Unity 3D

August 9th, 2009

Unity 3D is really geared toward visually building your game through use of the Unity 3D editor. However, there are times when you want to instantiate a prefab that you don’t already have in the Scene.

Instantiating a prefab from your library is easy, once you know how. Figuring out how is a pain. The biggest quirk is that your prefab must be located in a directory called “Resources” if you want to instantiate it using C#.

Here are the steps.

1. Make sure you create a directory called “Resources” and place it in your Assets/ directory.
2. Create a prefab. Let’s all it “FooPrefab”.
3. In C#, instantiate the prefab as follows:

1
GameObject go = Instantiate(Resources.Load("FooPrefab")) as GameObject;

Why does the prefab have to be placed in a directory called “Resources”.
Who knows?

C#, Unity 3D ,

  1. August 9th, 2009 at 23:46 | #1

    You can also reference unplaced prefabs by defining them as variables of the GameObject type in the script.

  2. August 10th, 2009 at 08:45 | #2

    When you link objects in the editor, Unity knows that they are in use. When you build your binary it knows which assets to include and which ones to leave out. Thus you can work with thousands of assets and only the ones that are actually needed are included in the build.

    Runtime resources that you want to load dynamically are placed in the Resource folder, as you found. Unity includes *everything* in that folder in the final build. So you get the dynamic control you sometimes want, and can also replace resources such as textfiles or textures without needing to rebuild the application. Downsides: you have to manually remove assets you don’t need, and I think the files are stored “in the clear”, not encrypted or bundled, easily accessible to someone who looks closely at your app.

  3. Lucas Meijer
    August 10th, 2009 at 09:44 | #3

    Darrel’s method is preferrable, because the everything in the resources folder bypasses the system that figures out what is used when, so it has to be 100% downloaded before your first scene starts.

    Note you can also use darrel’s method, and fill that reference trough an editorscript (lookup AssetDatabase.LoadAssetFromPath, instead of by drag&dropping).

  4. August 10th, 2009 at 13:15 | #4

    Thanks for the tips, guys. I know dragging and dropping objects to establish links is the Unity way. However, I like to do things with 100% code as much as possible. One reason is understandability. The other reason is I can use SVN with the code.

  5. Ren
    December 18th, 2009 at 08:03 | #5

    Putting the assets in the Resources folder worked alright.
    But how could I attach scripts to the newly created object?

  6. January 28th, 2010 at 10:26 | #6

    Agreed with Michael, the in-editor method is only preferable where you want to place entities in a level and edit their properties. Classic example being triggers to open doors, exploding barrels, etc.

    However, when you want assets that aren’t placed in the level to be included, you would be SOL. Take, for instance, if my player has 2 weapons that he always own, you’d want to always include all assets related to them. And just place a player spawn in the level. This merely one example. There’s also the case of wanting things to be more strongly typed in code, instead of just referring to a prefab which may point to any which object.

  7. February 17th, 2010 at 19:31 | #7

    Since people are saying using the in game editor is preferable for this kind of use case, I think I have a solution that’s kinda ok. I made an Empty Object in game called RuntimeAssets and attached a script called RuntimeAssets that looks like this

    public class RuntimeAssets : MonoBehaviour {

    private static RuntimeAssets bundle;
    public static RuntimeAssets getBundle(){
    if(bundle == null){
    bundle = GameObject.Find(”RuntimeAssets”).GetComponent();
    }
    return bundle;
    }

    public GameObject projectile_1;
    public GameObject projectile_2;
    public GameObject highlighter;

    }

    I can assign projectile_1, projectile_2, etc to prefabs in the editor, and from code I can call RuntimeAssets.getBundle().projectile_1 to dynamically assign.

  8. Jason
    March 27th, 2012 at 10:52 | #8

    Wow, thanks so much for this! I was looking for documentation on this exact concept and couldn’t find it. Thanks!

  1. February 9th, 2012 at 18:16 | #1