Hello, Unity
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 1 of 67.
Everything in a Unity game is a GameObject: the ground, the player, the camera, even an invisible spawner. What a GameObject does comes from the components attached to it, and the components you write yourself are scripts.
A script is a C# class that extends MonoBehaviour. There is no Main method: Unity runs the game and calls your methods. It calls Start() once, when the game begins, and Update() on every frame, many times a second. That is how a game moves: a tiny change in every frame.
void Start()
{
Debug.Log("The game has started");
}Debug.Log is Unity's Console.WriteLine: it writes a message to the Console, the panel under the game. The script in this lesson is already finished: press Play and watch the cube come alive.
Challenge
EasyThis script is already finished. Press Play to watch the cube spin and float, and see its message in the Console. Then press Check.
Try it yourself
using UnityEngine;
public class HelloUnity : MonoBehaviour
{
public float spinSpeed = 120f;
void Start()
{
Debug.Log("Hello, Unity!");
}
void Update()
{
transform.Rotate(0, spinSpeed * Time.deltaTime, 0);
float height = 1f + Mathf.Sin(Time.time * 2f) * 0.5f;
transform.position = new Vector3(0, height, 0);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unity Scripting Basics
1Your First Script
Hello, UnityPlace the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube4Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot7Project: Catch the Star
The CatcherA Falling Star5Transforms and Hierarchy
Parents and ChildrenScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon