First Script
Script structure
In Unity, a script is used to control the behavior of GameObjects and define game logic. Scripts are written in C# and allow developers to create interactions, manage physics, handle inputs or control animations.
The line: using UnityEngine; imports the internal Unity libraries and their classes.
A script typically extends the MonoBehaviour class through inheritance. Thus we can override built-in Unity methods like:
Start(): Runs once when the object is initialized
Update(): Runs every frame, useful for real-time updates
First Script
Scene setup
Create a script
Association
Open a script
In the Assets window, double click on the script icon
The script opens in Visual Studio
Copy paste the following code:
using UnityEngine; public class BonusScript : MonoBehaviour { void Start() { } void Update() { transform.Rotate(0, 0.1f, 0); } }
The Rotate function makes the object rotate around the vertical axis
Press CTRL-S to save the script
Notice that this triggers the recompilation of the script in the Unity
Run
External variables
You can add variables to your script to parameterize its behavior. Moreover, you can « export » these variables into the Inspector to edit them directly without opening the source code.
Rotation speed
Unity uses degrees for rotation, so each time the Update() function is called, the cube rotates by 1 degree. However, we don’t know how many times per second this function is executed. Additionally, the execution rate constantly varies depending on the current cpu load.
To precisely control the rotation speed, we must use physics” law. Here, we recall the formula that gives the rotation angle based on the rotation speed and the time interval:
Unity provides the variable Time.deltaTime which corresponds to dT.
In Visual Studio, create a public float variable RotSpeed
In the Update() function, apply the given formula
Select the cube and scale it to obtain a tube shape
In the Inspector, look at the script section
Enter Play mode
![]()
The bar makes one full turn every 2 seconds
Change the rotation speed value to 360
![]()
The bar makes one full turn every second
Help in case nothing works
using UnityEngine; public class BonusScript : MonoBehaviour { public float rotSpeed; void Start() { } void Update() { transform.Rotate(0,rotSpeed * Time.deltaTime, 0); } }Avertissement
You can interactively modify any values in the Inspector during Play mode. This is very useful for testing and debugging. However, keep in mind that any changes made in Play mode will be lost once you exit. Only values set in Edit mode are permanently retained.
Blinking light
Exit Play mode
Add two external variables: public Material
To retrieve the elapsed time since the start of the game, use the Time.time float variable
Every second, change the cube’s material:
Renderer r = GetComponent<Renderer>();
r.material = …
Help in case nothing works
using UnityEngine; public class BonusScript : MonoBehaviour { public float rotSpeed = 360.0f; public Material m1; public Material m2; void Start() { } void Update() { transform.Rotate(0,rotSpeed * Time.deltaTime, 0); int v = (int)(Time.time); Renderer r = GetComponent<Renderer>(); if (v % 2 == 0) r.material = m1; else r.material = m2; } }