ASU Learning Sparks

Defining GameObjects With Unity Scripting

Programmers use Unity scripting components and MonoBehaviours to define virtual world object interactions. The Unity game engine offers a large selection of standard components like video and audio playback as well as custom interactions available through MonoBehaviours. Both allow programmers to bring GameObjects to life and perform many different actions. Programmers can ...

Programmers use Unity scripting components and MonoBehaviours to define virtual world object interactions. The Unity game engine offers a large selection of standard components like video and audio playback as well as custom interactions available through MonoBehaviours. Both allow programmers to bring GameObjects to life and perform many different actions.

Programmers can instill different behaviors on objects in the virtual world. 

What should the object do when it first comes into being?

What should the object do on every frame tick as the display updates?

How should the object react to a user trying to interact with it?

How should the object react when other objects come near? 

In the Unity game engine, these questions can be answered by “Components,” and Unity comes with a wide base set of Components to program these interactions without touching any code. Attaching some components to a GameObject can enable video playback, audio playback, physics, and add interactive capabilities. However, Unity also allows programmers to specify their own kinds of interactions through scripting “MonoBehaviours.”

In a MonoBehaviour, the Unity Engine calls the Start function when the instance of the behavior object comes into being. It calls the Update function on every display frame. Programmers then get to decide what to do in those functions. Should the object be a certain color? Should it move? Should it react to a user’s position? Should it turn invisible? The programmer can control all of these behaviors and more. And if the programmer wishes, they can put this in the Start function to happen when the object first loads. Or they can put this code in the Update function to continuously update it every time the game engine calls for an update. 

In a MonoBehaviour, each component that is attached to a GameObject maintains an understanding of its own state variables, called properties. If the same MonoBehaviour script is attached to different GameObjects, the properties are still independent from one another. This means the developer gets to write these MonoBehaviour scripts as reusable recipes for the GameEngine to follow, and then configure each Component independently for different GameObjects, depending on the situation for each GameObject.

This allows for reusable programmable behavior for the GameObjects, giving developers the power to have objects create activity, respond to interactions, or do many other operations.