Unity C# Cheat Sheet
A one-page printable reference for MonoBehaviour lifecycle, input, physics, UI, coroutines, scene management and the most-used Unity C# patterns. Pin it next to your monitor!
A one-page printable reference for MonoBehaviour lifecycle, input, physics, UI, coroutines, scene management and the most-used Unity C# patterns. Pin it next to your monitor!
void Awake() { // runs on object creation } void OnEnable() { // runs when object becomes active } void Start() { // runs before first frame } void Update() { // runs every frame } void FixedUpdate() { // runs on physics step (50 Hz) } void LateUpdate() { // runs after all Update calls } void OnDisable() { // runs when object deactivated } void OnDestroy() { // runs when object destroyed }
| Input.GetKey(KeyCode.A) | Held down |
| Input.GetKeyDown() | Pressed this frame |
| Input.GetKeyUp() | Released this frame |
| Input.GetAxis("Horizontal") | -1 to 1 smoothed |
| Input.GetAxisRaw() | -1 or 1 no smoothing |
| Input.GetButton("Jump") | Virtual button held |
| Input.GetButtonDown() | Virtual button pressed |
| Input.GetMouseButton(0) | Left mouse held |
| Input.GetMouseButtonDown(0) | Left click this frame |
| Input.mousePosition | Mouse screen position |
| Input.touchCount | Number of touch points |
| Input.GetTouch(0) | Touch data by index |
| Rigidbody2D / Rigidbody | Physics body component |
| AddForce(Vector2) | Apply force to rigidbody |
| rb.linearVelocity | Current velocity vector (Unity 6) |
| Physics2D.Raycast() | 2D ray collision test |
| Physics.Raycast() | 3D ray collision test |
| OnCollisionEnter2D() | 2D collision callback |
| OnTriggerEnter2D() | 2D trigger callback |
| OnCollisionEnter() | 3D collision callback |
| OnTriggerEnter() | 3D trigger callback |
| rb.isKinematic | Disable physics simulation |
// Get existing component var rb = GetComponent<Rigidbody2D>(); // Find in children var sr = GetComponentInChildren<SpriteRenderer>(); // Add new component var col = AddComponent<BoxCollider2D>(); // Spawn prefab var obj = Instantiate(prefab, position, rotation); // Remove object Destroy(gameObject); Destroy(gameObject, 2f); // delayed // Find by name / tag GameObject.Find("Enemy"); GameObject.FindWithTag("Player"); GameObject.FindGameObjectsWithTag("Coin");
| Text (TMP) | Text display component |
| Image | Sprite/image display |
| Button | Clickable UI button |
| Slider | Value slider (0-1) |
| InputField | Text input box |
| Canvas | Root UI container |
| RectTransform | UI positioning/sizing |
| CanvasGroup | Fade/show/hide UI block |
| ScrollRect | Scrollable content area |
// Update text GetComponent<TextMeshProUGUI>().text = "Score: " + score; // Fade out UI canvasGroup.alpha = 0f; canvasGroup.interactable = false;
using System.Collections; IEnumerator SpawnWave() { yield return new WaitForSeconds(2f); Instantiate(enemy, spawnPos, Quaternion.identity); yield return null; // wait one frame yield return new WaitUntil(() => ready); yield return new WaitWhile(() => !ready); } // Start / Stop StartCoroutine(SpawnWave()); StopCoroutine(SpawnWave()); StopAllCoroutines();
using UnityEngine.SceneManagement; // Load scene SceneManager.LoadScene("Level2"); SceneManager.LoadScene(1); // by build index // Async load var op = SceneManager.LoadSceneAsync("Level2"); yield return op; // Reload current scene SceneManager.LoadScene( SceneManager.GetActiveScene().buildIndex); // Quit app Application.Quit(); // Application focus / pause Application.targetFrameRate = 60;
| Vector2(x, y) | 2D vector |
| Vector3(x, y, z) | 3D vector |
| Mathf.Lerp(a, b, t) | Linear interpolation |
| Mathf.Clamp(val, min, max) | Restrict to range |
| Mathf.Abs(n) | Absolute value |
| Mathf.Pow(b, e) | Power / exponent |
| Mathf.Sqrt(n) | Square root |
| Mathf.Round(n) | Round to nearest int |
| Quaternion.Euler(x, y, z) | Euler to rotation |
| Vector3.Distance(a, b) | Distance between points |
โโ PLAYER CONTROLLER (2D) โโ float speed = 5f; float jumpForce = 10f; Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { float move = Input.GetAxisRaw("Horizontal"); rb.linearVelocity = new Vector2(move * speed, rb.linearVelocity.y); if (Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(rb.linearVelocity.y) < 0.01f) rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); } โโ CAMERA FOLLOW โโ Transform target; float smoothSpeed = 0.125f; void LateUpdate() { var pos = new Vector3(target.position.x, target.position.y, transform.position.z); transform.position = Vector3.Lerp(transform.position, pos, smoothSpeed); } โโ HEALTH SYSTEM โโ public int maxHealth = 100; int currentHealth; public void TakeDamage(int amount) { currentHealth -= amount; if (currentHealth <= 0) { Die(); } } โโ SPAWNING โโ public GameObject prefab; public float spawnRate = 2f; void Start() { InvokeRepeating("Spawn", 0f, spawnRate); } void Spawn() { Instantiate(prefab, new Vector3(0, 7, 0), Quaternion.identity); } โโ SAVE SYSTEM โโ PlayerPrefs.SetInt("Score", score); int s = PlayerPrefs.GetInt("Score"); PlayerPrefs.SetString("PlayerName", name); PlayerPrefs.Save();
Follow one of our free step-by-step workshops in the Workshop hub.
๐ง Browse Workshops โ