Assert
The Assert class contains assertion methods for setting invariants in the code.
Read time 1 minuteLast updated 6 days ago
Definition
- Type: Class
- Namespace: UnityEngine.Assertions
- Assembly: UnityEngine.CoreModule
public static class Assert
Remarks
Calls to all methods in this class are only conditionally included in a Development
Build or if the ManagedCodeVariant is set to ManagedCodeVariant.Checked or ManagedCodeVariant.Debug. For more information, refer to ManagedCodeVariant. The inclusion of an assertion is controlled by the define.
UNITY_ASSERTIONSAssert throws exceptions whenever an assertion fails.
If a debugger is attached to the project ( is true), AssertionException is thrown to pause the execution and invoke the debugger.
System.Diagnostics.Debugger.IsAttachedExamples
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
Method | Description |
|---|---|
| AreApproximatelyEqual | Assert the values are approximately equal. |
| AreEqual | Assert that the values are equal. |
| AreNotApproximatelyEqual | Asserts that the values are approximately not equal. |
| AreNotEqual | Assert that the values are not equal. |
| IsFalse | Return |
| IsNotNull | Assert that the value is not null. |
| IsNull | Assert the value is null. |
| IsTrue | Asserts that the condition is true. |