Singapore-MIT GAMBIT Game Lab spacer
spacer New Entries Archives Links subheader placeholder
left edge
More about using objects in a script

Remember back in the "Instantiating prefabs" entry, when I mentioned that Unity doesn't like casting instances of a prefab (which are of type Object) to GameObject? This has gone from a minor annoyance to an actual problem. A lot of the things I want to do to these objects require GameObject functions.

I did find a way around it, though. For objects like the walls and floor, I don't know how many I'm going to need, so I need to create them from the script. But for objects like the player avatar, since I'm only going to make one, I don't need to know the prefab; all I have to do is create one ahead of time, then move it to the appropriate location using the script. So instead of having a Transform object in the script and setting it to the prefab, the script now has instance variable

public GameObject avatar;

Then I simply created an instance of the avatar in Unity and dragged it from the hierarchy to the inspector. The script will use the same avatar every time it sets up a level.

(Update: Someone on the Unity forums has come up with the answer! Just in time, too, since I can't use this workaround when creating the chasers.)

right edge
bottom curves