Version: 2020.1
LanguageEnglish
  • C#

Assert

class in UnityEngine.Assertions

/

Implemented in:UnityEngine.CoreModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

The Assert class contains assertion methods for setting invariants in the code.

All method calls will be conditionally included only in a development build, unless specified explicitly. See BuildOptions.ForceEnableAssertions. The inclusion of an assertion is controlled by the UNITY_ASSERTIONS define.

Assert throws exceptions by default whenever an assertion fails. You can however set Assertions.Assert._raiseExceptions to false and Unity then logs a message using LogType.Assert instead.

If a debugger is attached to the project (System.Diagnostics.Debugger.IsAttached is true), AssertionException will be thrown in order to pause the excecution and invoke the debugger.

using UnityEngine;
using UnityEngine.Assertions;

public class AssertionExampleClass : MonoBehaviour { public int health; public GameObject go;

void Update() { // You expect the health never to be equal to zero Assert.AreNotEqual(0, health);

// The referenced GameObject should be always (in every frame) be active Assert.IsTrue(go.activeInHierarchy); } }

Static Methods

AreApproximatelyEqualAssert the values are approximately equal.
AreEqualAssert that the values are equal.
AreNotApproximatelyEqualAsserts that the values are approximately not equal.
AreNotEqualAssert that the values are not equal.
IsFalseReturn true when the condition is false. Otherwise return false.
IsNotNullAssert that the value is not null.
IsNullAssert the value is null.
IsTrueAsserts that the condition is true.

Did you find this page useful? Please give it a rating: