Learn UNITY with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
Unity Simple 2D Todo Game Prototype
// C# script: TodoItem.cs
using UnityEngine;
public class TodoItem : MonoBehaviour {
public string taskName;
void OnMouseDown() {
Debug.Log("Task completed: " + taskName);
}
}
// Usage: Attach TodoItem.cs to game objects representing tasks in the Unity scene.
Demonstrates a simple Unity scene simulating a Todo game concept, where users can interact with objects representing tasks in a 2D environment.
Unity Simple Click Counter
// C# script: ClickCounter.cs
using UnityEngine;
public class ClickCounter : MonoBehaviour {
private int clicks = 0;
void OnMouseDown() {
clicks++;
Debug.Log("Clicked " + clicks + " times.");
}
}
Demonstrates counting clicks on game objects using Unity’s OnMouseDown event.
Unity Player Movement 2D
// C# script: PlayerMovement.cs
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 5f;
void Update() {
float moveX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float moveY = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(moveX, moveY, 0);
}
}
Implements simple player movement using arrow keys in a Unity 2D scene.
Unity Object Spawner
// C# script: ObjectSpawner.cs
using UnityEngine;
public class ObjectSpawner : MonoBehaviour {
public GameObject prefab;
void Update() {
if(Input.GetMouseButtonDown(0)) {
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
Spawns prefab objects dynamically when the user clicks in the scene.
Unity Collectible Item System
// C# script: Collectible.cs
using UnityEngine;
public class Collectible : MonoBehaviour {
void OnTriggerEnter2D(Collider2D col) {
if(col.CompareTag("Player")) {
ScoreManager.instance.AddScore(1);
Destroy(gameObject);
}
}
}
// C# script: ScoreManager.cs
using UnityEngine;
public class ScoreManager : MonoBehaviour {
public static ScoreManager instance;
int score = 0;
void Awake() { instance = this; }
public void AddScore(int amount) {
score += amount;
Debug.Log("Score: " + score);
}
}
Implements a collectible system where the player collects coins and updates the score.
Unity Simple Timer
// C# script: Timer.cs
using UnityEngine;
public class Timer : MonoBehaviour {
public float timeLeft = 10f;
void Update() {
timeLeft -= Time.deltaTime;
if(timeLeft <= 0) {
Debug.Log("Time's up!");
enabled = false;
}
}
}
Shows a countdown timer and logs when time runs out.
Unity Follow Camera
// C# script: FollowCamera.cs
using UnityEngine;
public class FollowCamera : MonoBehaviour {
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
void LateUpdate() {
if(target == null) return;
Vector3 desired = target.position + offset;
transform.position = Vector3.Lerp(transform.position, desired, smoothSpeed);
}
}
A camera script that smoothly follows the player in a 2D game.
Unity Basic Jump Controller
// C# script: JumpController.cs
using UnityEngine;
public class JumpController : MonoBehaviour {
public float jumpForce = 5f;
private Rigidbody2D rb;
void Start() {
rb = GetComponent<Rigidbody2D>();
}
void Update() {
if(Input.GetKeyDown(KeyCode.Space)) {
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
}
Implements simple jump physics for a 2D player using Rigidbody2D.
Unity Simple Enemy AI
// C# script: EnemyAI.cs
using UnityEngine;
public class EnemyAI : MonoBehaviour {
public Transform player;
public float speed = 2f;
public float detectionRange = 5f;
void Update() {
float dist = Vector2.Distance(transform.position, player.position);
if(dist < detectionRange) {
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
}
}
Demonstrates a simple AI that follows the player when within a certain distance.
Unity Scene Switcher
// C# script: SceneSwitcher.cs
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneSwitcher : MonoBehaviour {
public string sceneName;
void Update() {
if(Input.GetKeyDown(KeyCode.Return)) {
SceneManager.LoadScene(sceneName);
}
}
}
Switches between scenes using UnityEngine.SceneManagement.