Join Adam Crespi for an in-depth discussion in this video Adding scripts to GameObjects, part of Unity 4.3 Essential Training.
- View Offline
In Unity, scripting provides a powerful tool set to extend and enhance the functionality of the game. We can even extend and enhance the functionality of Unity itself. And it supports three different scripting languages plus a shader language to do so. To attach a script to a game object, like we did with the first person controller, we can simply drag it in as a component. I'll put a new blank script on one of my objects to start out. For example, I'll pick the master building and add a component. In the Components, I'll choose New Script.
MonoDevelop is a powerful scripting program that ships with unity and supports multiple languages. It also supports code hinting and syntax coloring. It will help you along as much as it can and really let you get in and get the best out of your scripting. What this says, for example now, is that when the game starts, function start, do something. In this case nothing. And then update every frame, do something and again, nothing. We're ready to add pieces into this script and it's that simple to attach scripts to an object.
We can either make scripts by choosing new components or we can make a new script right in the assets window and then drag it onto an object. I'll close this script sink in MonoDevelop and come back and look at creating a new script. I'll right click in the Assets window, and choose Create. And here's a C sharp. I'll name this C sharp, rise. And there's that C sharp script selected in the inspector, showing, well, what's going on so far. In this case, void start, nothing, and void update, nothing.
When it starts out the game, initializes, do something and once per frame, do something. That's the basics of it. We can make a script to do just about anything we'd like. Anything from culling animations through extending functionality when a character collides with something If you've used a first-person controller, you're already using scripts. And it's that simple to get them implemented. There's no limit to what you can do with the script. So get in and explore. And start to make scripts that enhance the functionality of your game.
- Designing the game
- Creating and transforming objects
- Importing and configuring models and textures
- Setting properties in the Inspector
- Creating the terrain geometry
- Building materials and adding shaders
- Creating GameObjects
- Exploring physics
- Animating objects
- Lighting the scene
- Creating 2D game elements
- Adding special effects