Singapore-MIT GAMBIT Game Lab spacer
spacer New Entries Archives Links subheader placeholder
left edge
Instantiating prefabs

I'm working on getting Unity to start with an empty screen and build everything from scratch with scripts. I've quickly discovered that it's not possible to build everything from scratch. Prefabs are very helpful (you can see the Unity documentation on creating them here), but it's still necessary to use the inspector to some degree.

For example, I built a prefab of the player. I can notify the script that it needs to create a prefab by including it as an instance variable:

public Transform avatar;

and then creating an instance of it in the code:

Object ob = Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);

(The Vector3 gives the initial location.) However, you may notice that the prefab is never assigned a value. It is necessary to go to the project view and click and drag the appropriate prefab onto the variable name in the Inspector. For this reason, it's important for the instance variable to be made public, because otherwise it will not appear in the Inspector.

The nice thing about this is that the prefab comes with the player's scripts and components already attached. The same goes for other prefab objects in the scene, which means that once I get the script to instantiate all objects, I can immediately start playing, since the keyboard input functions are in the player script.

Important note: When creating an instance of a prefab this way, be very careful about making changes to it in the script! I found out the hard way that if you make changes to avatar instead of ob, the prefab itself changes, which can make wild and wacky things happen if you try to run the script more than once--especially if you want to, say, scale the object. This has become something of a headache, because for some reason Unity makes a fuss about casting ob to a GameObject, which would make it easier to work with.

The Instantiate function is similar to (but not quite the same as) the CreatePrimitive function, which is the equivalent function for creating primitive shapes such as planes and cubes. A big advantage of CreatePrimitive is that the object it returns is already a GameObject, avoiding the casting problem.

right edge
bottom curves