Introduction: Why Learn Unity Scripting?
Unity's visual tools — the scene editor, animator, physics system — are powerful. But to build actual game logic, you need C# scripting. Don't let that intimidate you. Unity's scripting API is approachable, well-documented, and follows consistent patterns once you understand the basics. This guide walks you through everything you need to write your first working Unity script.
Setting Up Your Script
In Unity, scripts are created by right-clicking in the Project window → Create → C# Script. Name it descriptively (e.g., PlayerController). Double-clicking the file opens it in your default code editor — Visual Studio or VS Code are both excellent choices.
A fresh Unity script looks like this:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
void Start() { }
void Update() { }
}
Every Unity script inherits from MonoBehaviour, which is what allows it to attach to GameObjects and participate in the game loop.
The MonoBehaviour Lifecycle
Understanding when Unity calls your code is fundamental:
- Awake(): Called once when the object is first created, even if the component is disabled. Use for internal initialization.
- Start(): Called once before the first frame update, after all Awake calls. Use for setup that depends on other objects.
- Update(): Called every frame. Use for input polling, movement logic, timers.
- FixedUpdate(): Called at a fixed time interval (default 50 times/sec). Use for physics operations with Rigidbody.
- OnDestroy(): Called when the object is destroyed. Use for cleanup.
Variables and the Inspector
One of Unity's best features is that public variables automatically appear in the Inspector, letting you tweak values without editing code:
public float moveSpeed = 5f;
public int health = 100;
public GameObject bulletPrefab;
Use [SerializeField] to expose private variables in the Inspector while keeping them encapsulated:
[SerializeField] private float jumpForce = 8f;
Reading Player Input
Unity's new Input System is recommended for new projects, but the classic approach is still valid and simpler to learn first:
void Update()
{
float horizontal = Input.GetAxis("Horizontal"); // -1 to 1
float vertical = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
Moving a GameObject
There are two main ways to move objects in Unity:
Transform-Based (no physics)
void Update()
{
float h = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * h * moveSpeed * Time.deltaTime);
}
Rigidbody-Based (with physics)
private Rigidbody rb;
void Start() { rb = GetComponent<Rigidbody>(); }
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
rb.MovePosition(rb.position + Vector3.right * h * moveSpeed * Time.fixedDeltaTime);
}
Rule of thumb: If the object has a Rigidbody, use physics methods. Otherwise, use Transform.
Debugging Your Scripts
Use Debug.Log() liberally while learning — it prints messages to the Console window:
Debug.Log("Player jumped!");
Debug.Log("Health is now: " + health);
Next Steps
Once you're comfortable with these fundamentals, explore:
- Coroutines for time-based logic
- ScriptableObjects for data-driven design
- Events and delegates for decoupled systems
- The Unity Input System package for modern input handling
Practice by modifying one of the free Unity projects available on this site — reading real project code accelerates learning faster than tutorials alone.