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.
A failure of an assertion method does not break the control flow of the execution.
On a failure, an assertion message is logged (LogType.Assert) and the execution
continues. If Assert.raiseExceptions is set to true, an AssertionException
is thrown instead of logging a message.
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); } }
raiseExceptions | Whether Unity should throw an exception on a failure. |
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 true when the condition is false. Otherwise return false. |
IsNotNull | Assert that the value is not null. |
IsNull | Assert the value is null. |
IsTrue | Asserts that the condition is true. |
Did you find this page useful? Please give it a rating: