Back to Writing
NOTESunitycsharpdebugginggame-devassertions
Unity - Using the Assert Class for Debugging
June 5, 2022•Updated Feb 17, 2026
Unity's Assert class is used for assertion-based debugging.
Syntax:
Assert.IsTrue(condition);
Assert.IsFalse(condition);
Assert.AreEqual(expected, actual);
Assert.IsNull(obj);
Assert.IsNotNull(obj);
Example:
void Update() {
Assert.IsNotNull(player);
Assert.IsTrue(health > 0);
}
Assertions are only evaluated in the Editor and Development Builds. They are stripped from Release Builds.
Benefits:
- Catch logic errors early
- Document expected conditions
- Aid in debugging
Note: Do not use assertions for handling user input or expected errors. Use try-catch or validation logic instead.