Unity & C# Cheat Sheet
A one-page printable reference for variables, MonoBehaviour lifecycle, components, input and the most-used C# snippets in Unity. Pin it next to your monitor!
A one-page printable reference for variables, MonoBehaviour lifecycle, components, input and the most-used C# snippets in Unity. Pin it next to your monitor!
public int health = 100; public string playerName = "Pip"; public float speed = 4.5f; private bool isAlive = true; const int MAX_HP = 100; [SerializeField] private float jumpForce = 12f;
Use [SerializeField] to expose a private field in the Inspector. public fields show automatically.
void Awake() { // runs once, before Start } void Start() { // runs once, before first frame } void Update() { // runs every frame transform.Translate(Vector3.right * speed * Time.deltaTime); } void FixedUpdate() { // runs on a fixed physics timestep }
if (health <= 0) { Destroy(gameObject); } else if (health < 20) { Debug.Log("Low HP!"); } else { Debug.Log("OK"); } foreach (GameObject enemy in enemies) { Destroy(enemy); } for (int i = 0; i < 10; i++) { } while (ammo > 0) { ammo--; }
public static event Action<int> OnHealthChanged; void TakeDamage(int amount) { health -= amount; OnHealthChanged?.Invoke(health); } // subscribe elsewhere: void OnEnable() { Player.OnHealthChanged += UpdateHpLabel; } void OnDisable() { Player.OnHealthChanged -= UpdateHpLabel; }
C# events decouple systems one script broadcasts, others react without direct references.
void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector3 dir = new Vector3(h, 0, v).normalized; rb.MovePosition(transform.position + dir * speed * Time.deltaTime); if (Input.GetButtonDown("Jump") && isGrounded) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); } }
| GetComponent<T>() | Get a component on this object |
| transform.position | World-space position |
| transform.Find("Name") | Find a child by name |
| Instantiate(prefab) | Spawn a copy of a prefab |
| Destroy(obj) | Remove a GameObject |
| gameObject.SetActive(false) | Enable/disable an object |
| tag, CompareTag() | Identify object types |
| Transform | Position, rotation, scale |
| Rigidbody / Rigidbody2D | Physics-driven movement |
| Collider / Collider2D | Collision & trigger shapes |
| SpriteRenderer | Displays a 2D sprite |
| Animator | Plays animation states |
| AudioSource | Plays sound effects/music |
| Canvas / RectTransform | UI layout root & positioning |
| NavMeshAgent | AI pathfinding movement |
StartCoroutine(MyRoutine()) โ begin a coroutineyield return new WaitForSeconds(1f); โ wait one secondyield return null; โ wait one frameTime.deltaTime โ seconds since last frameTime.timeScale = 0; โ pause the gameInvokeRepeating("Tick", 1f, 1f) โ call a method repeatedly// Load a scene SceneManager.LoadScene("Level2"); // Random number / pick int roll = Random.Range(1, 7); // 1-6 string pick = options[Random.Range(0, options.Length)]; // Save / load a value PlayerPrefs.SetInt("score", 10); int score = PlayerPrefs.GetInt("score", 0); // Find objects by tag GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Raycast if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 2f)) { Debug.Log(hit.collider.name); }
Follow one of our free step-by-step workshops in the Workshop hub.
๐ง Browse Workshops โ