Unity C# Syntax, Code Examples, and Usage Guide
Category Syntax / Code Example Use / Purpose
Basic Script Setup using UnityEngine; All Unity scripts derive from
MonoBehaviour.
public class Player : MonoBehaviour { }
Start Method void Start() { Called once before the first
Debug.Log("Game Started"); frame update.
}
Update Method void Update() { Called once per frame — used
MovePlayer(); for movement, input, etc.
}
FixedUpdate void FixedUpdate() { Called on a fixed time step —
rb.AddForce(Vector3.up); ideal for physics updates.
}
LateUpdate void LateUpdate() { Runs after all Update() calls —
camera.Follow(player); great for camera tracking.
}
Awake void Awake() { Called when script instance is
playerHealth = 100; loaded — before Start().
}
Variables / Fields public float speed = 5f; Define public (visible in
private int score = 0; Inspector) and private fields.
SerializeField [SerializeField] Makes private fields visible in
private int health = 100; the Inspector.
GameObject Access GameObject enemy = GameObject.Find("Ene Find GameObjects by name in
my"); the scene.
Component Access Rigidbody rb = GetComponent(); Get attached components (like
Rigidbody, Collider, etc.).
Instantiate / Destroy Instantiate(prefab, position, rotation) Create or remove
; GameObjects during
Destroy(gameObject);
gameplay.
Transform transform.position += Vector3.forward * Move, rotate, or scale
Manipulation speed * Time.deltaTime; GameObjects in 3D space.
Input Handling float h = Input.GetAxis("Horizontal"); Capture player input
(keyboard, mouse, joystick).
Collision Detection void OnCollisionEnter(Collision collisi Detect collisions with physical
on) { objects.
Debug.Log(collision.gameObject.name
);
}
Trigger Detection void OnTriggerEnter(Collider other) { Detect non-physical trigger
Debug.Log("Entered trigger!"); interactions.
}
Category Syntax / Code Example Use / Purpose
Coroutines IEnumerator Wait() { Run asynchronous sequences
yield return new WaitForSeconds(2f) of events.
;
}
StartCoroutine StartCoroutine(Wait()); Start a coroutine method.
Stopping Coroutines StopCoroutine("Wait"); Stop specific or all running
StopAllCoroutines(); coroutines.
Time.deltaTime transform.Translate(Vector3.forward * s Ensures smooth movement
peed * Time.deltaTime); across frame rates.
Physics (Rigidbody) rb.AddForce(Vector3.up * 5f, ForceMode. Apply forces to GameObjects
Impulse); using physics.
Raycasting if (Physics.Raycast(transform.position, Detect objects in a line from a
transform.forward, out hit)) { point.
Debug.Log(hit.collider.name);
}
UI Access myText.text = "Score: " + score; Modify UI text elements in
Unity.
Scene Management using UnityEngine.SceneManagement; Load or reload scenes.
SceneManager.LoadScene("Level2");
Application Control Application.Quit(); Quit the game (works in
builds).
PlayerPrefs PlayerPrefs.SetInt("Score", 100); Store and retrieve small
int s = PlayerPrefs.GetInt("Score"); persistent data.
Audio audioSource.Play(); Play sound attached to an
AudioSource component.
Gizmos void OnDrawGizmos() { Draw shapes in the editor for
Gizmos.DrawWireCube(transform.posit visualization.
ion, Vector3.one);
}
Invoke / Invoke("Shoot", 2f); Call a method after delay or
InvokeRepeating InvokeRepeating("Spawn", 1f, 3f); repeatedly.
Tag / Layer Check if (other.CompareTag("Enemy")) { } Identify objects via tag or layer.
DontDestroyOnLoad DontDestroyOnLoad(gameObject); Keep GameObject alive across
scene loads.
OnEnable / void OnEnable() { } Called when script is enabled
OnDisable void OnDisable() { } or disabled.
Debugging Debug.Log("Info"); Print logs to Unity Console for
Debug.LogWarning("Caution"); debugging.
Debug.LogError("Error");
Category Syntax / Code Example Use / Purpose
ScriptableObject [CreateAssetMenu(menuName="New Data")] Create reusable data
public class Data : ScriptableObject { containers in Unity.
}
Custom Inspector [CustomEditor(typeof(MyScript))] Modify how scripts appear in
public class MyScriptEditor : Editor { Unity Editor.
}
Attributes [Header("Player Settings")] Decorate Inspector with
[Range(0, 10)] custom UI controls.
public int health;