Component

How to use Components to add behaviour/functionality to GameObjects in Unity!

Components attach to GameObjects to give them functionality/behaviour.

GameObject
Component
Component
Component
Pikachu
Pokemon
Abilities
Stats

A component is an Instance of a Class that exists in the Scene.

Custom components can be created to build any functionality you require in your game.

Examples

Create a custom component

Custom components are created by coding a Class, and then making it inherit from MonoBehaviour. This allows you to click-drag it onto any GameObject or Prefab, and gives the class access to MonoBehaviours’ features.

public class Player : MonoBehaviour {
	
	[Header("Settings")]
	public string _name; // This will show in the Inspector when dragged onto a GameObject.
	
	void Start()
	{
		Debug.Log("This Start function only exists in MonoBehaviour!");
	}
	
	void Update()
	{
		Debug.Log("This Update function only exists in MonoBehaviour!");
	}

}

Getting a component on the current GameObject

Building a physics object from scratch

Rigidbody is a Unity Component that allows the physics engine to take control of your GameObject.

Sphere Collider is a Unity Component that allows your GameObject to bump into things, so it won’t fall through the floor.

Ball
SphereCollider
RigidBody

By adding both these components to a GameObject, it will create a realistic ball that will roll with physics.

Building a dog

Components allow you to build objects from modular pieces, so, to make a dog, you wouldn’t necessarily have a Component named “Dog”.

Instead, we build it out of Components that add behaviour, and attach them to the GameObject that represents the dog.

Bark
Bark()
Jump
Jump()
Microchip
ownerName = "Bob Smith"
ownerHouseNumber = 100
ownerAddress = "Beverly Hills"
ownerZipCode = "90210"

Bark is a custom Component that contains a Bark() function.

public class Bark : MonoBehaviour {
	
	void Bark()
	{
		Debug.Log("Barked!");
	}

}

Jump is a custom Component that contains a Jump() function.

public class Jump : MonoBehaviour {
	
	void Jump()
	{
		Debug.Log("Jumped!");
	}

}

Microchip is a custom Component that contains microchip data about the dog’s owners.

public class Microchip : MonoBehaviour {
	
	[Header("Info")]
	public string _ownerName = "Bob Smith";
	public int _ownerHouseNumber = 100;
	public string _ownerAddress = "Beverly Hills";
	public string _ownerZipCode = "90210";
	
}

Building a Player character

CharacterController is a Unity Component that contains a useful Move() function which allows us to collide with objects while moving.

Player
CharacterController
Move()
MoveHorizontal
CharacterController
cc
Awake()
Update()
CharacterInfo
string
firstName
string
secondName
int
age

Player is a custom Component that simply ‘tags’ a GameObject as the player. It’s used instead of Unity’s tag system.

public class Player : MonoBehaviour { }

MoveHorizontal is a custom Component that stores Input values and puts them into the CharacterController’s Move() function.

public class MoveHorizontal : MonoBehaviour {
	
	[Header("References")];
	private CharacterController _cc;
	
	void Awake()
	{
		// Store CharacterController component
		_cc = GetComponent<CharacterController>();
	}
	
	void Update()
	{
		// Create a float to store input value from A/D keys, left/right arrow keys, and left analog stick.
		float h = Input.GetAxis("Horizontal");
		
		// Create a Vector3 to store our direction. h moves us on the X axis, and v moves us on the Y axis.
		Vector3 moveDirection = new Vector3(h, 0f, 0f);
		
		// Apply movement
		_cc.Move(moveDirection);
	}

}

CharacterInfo lets us give our player a name, etc. It can also attach to any other character that needs a name.

public class CharacterInfo : MonoBehaviour {
	
	[Header("Info")];
	public string _firstName;
	public string _secondName;
	public int _age;

}

Notes

  • Public member variables will be initialized automatically by the editor.
    • public List<int> myList; will work.
    • public List<int> myList = new List<int>() is still recommended.