Scripts as Behaviour Components
Tutorial
·
Beginner
·
+10XP
·
5 mins
·
(16028)
Unity Technologies

What are Scripts in Unity? Learn about the behavior component that is a Unity script, and how to Create and Attach them to objects.
This tutorial is included in the Beginner Scripting project.
Next: Variables and Functions
Languages available:
1. Scripts as Behaviour Components
C# Scripts as Behaviour Components in Unity! - Beginner Scripting Tutorial
using UnityEngine;
using System.Collections;
public class ExampleBehaviourScript : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
GetComponent<Renderer> ().material.color = Color.red;
}
if (Input.GetKeyDown(KeyCode.G))
{
GetComponent<Renderer>().material.color = Color.green;
}
if (Input.GetKeyDown(KeyCode.B))
{
GetComponent<Renderer>().material.color = Color.blue;
}
}
}