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 » 2009» August» 9

Archive

Archive for August 9th, 2009

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 ,