C# for Games

C# gameplay foundations

Unity gameplay code benefits from small components with clear responsibilities. Avoid giant MonoBehaviours that own input, UI, physics, audio and persistence simultaneously.

csharp
public class Coin : MonoBehaviour
{
    [SerializeField] private int value = 1;

    public int Value => value;

    private void OnTriggerEnter(Collider other)
    {
        if (!other.CompareTag("Player")) return;
        Collect();
    }

    private void Collect() { /* ... */ }
}
  • Prefer private fields with SerializeField for Inspector configuration.
  • Use interfaces or events to reduce direct coupling.
  • Keep gameplay state separate from presentation where practical.
Lesson complete?Use the next lesson to continue the learning path.