Behavior Components

Component Oriented Architecture

Unity uses a Component Oriented Architecture which is a type of Object Oriented programming designed to reduce complexity and enhance modularity compared to traditional Inheritance structure for creating game entities. Components enable gameObject Behavior The idea is that most of the custom code that you'll write for Unity is designed to create a specific custom behavior for a gameObject. This means that it's easy to create slightly modified versions of a type of gameObject by simply using a different type of behavior component, such that an entire new child-class definition is not required. The most basic type of Unity scripting is creating custom gameObject behaviors, where most gameObjects have many different components that may interact with each other to control the gameObject's behavior.

Definition: A Unity GameObject is essentially an empty container that has a 3D world position specified in its Transform component. Multiple standard or custom components can be added to the GameObject to give it customized behaviors.

Behavior Components: Important Details:

  • All Unity Components must inherit from the MonoBehaviour base-class. When you create a C# script, MonoBehaviour is specified as the base-class by default. Any Component must have MonoBehaviour as it's base-class.

  • Component class variables defined within a component's class definition define attributes for the component, some of these can be configured to be editable in the inspector panel.

  • The Inspector panel displays and allows modification of components on a gameObject that is currently selected in the Hierarchy Panel.

Default, Empty GameObject - Transform Component

  • All gameObjects must have a Transform component which specifies x, y, z coordinates of it's position in the scene's world-space.

  • The default empty gameObject only has a Transform component, all other components must be manually added.

  • The Transform component of a Child gameObject is defined relative to its' Parent gameObject's Transform component. Any change to the Transform Component of a Parent gameObject will result in corresponding change to the Child gameObject.

  • UI-GameObjects (user interface) have a Rect-Transform component which provides the ability to define width, height, anchor-points relative to the default UI canvas or the parent UI-gameObject for a child UI-gameObject.

Pre-Configured, Prefab Components

  • Unity provides pre-configured GameObjects: The GameObject menu allows for creation of many types of standard, pre-configured gameObjects, in many cases, additional custom components are than added to the pre-configured gameObject: example 2D-Sprite, UI-Text

  • Prefab GameObjects:

The idea is that any type of GameObject can be created by adding custom behaviors added to it.

Adding Components to GameObjects: Unity provides many standard components but the ease of creating custom components to create custom behavior allows for a modular game design process. Components are added to a gameObject using the Drop-down menu at the bottom of the Inspector Panel

Example: an apple gameObject and a balloon gameObject might both have been created using the standard 2D-sprite gameObject. Their behaviors may differ due to each having the same type of physics RigidBody2D component with modified gravity attribute values, such that the apple falls and the balloon rises. For enhanced behavior, the apple might have a custom component that allows it to be collected and used to extend a player's health, while the balloon may have an Animator component that displays a pop animation when the balloon hits a time or height constraint.

Last updated