Assert function?

Is there an Assert function?

A function that gives an error or exception if the evaluated expression is false. It would be great all asserts are removed for release builds.

Here's a better way:

#define DEBUG

using System;
using System.Diagnostics;

public class DebugUtils
{
    [Conditional("DEBUG")]
    static void Assert(bool condition)
    {
        if (!condition) throw new Exception();
    }
}

That way, you could do something like:

DebugUtils.Assert(FindAllMiceInTheCity().Length < 1000000);

...without your code doing the potentially-costly code that generates the boolean passed to the Assert. Not that you'd want to do insanely costly tests while debugging either, but you get the idea...

I know this is an old question, but it shows up when searching for this on Google and there are new possibilities.
From the Unity 5.1 release notes:

New runtime assertion library under
UnityEngine.Assertions namespace. The
library provides a set of methods for
setting assertions in your code.

  • The method calls will be conditionally compiled out from
    non-developer’s builds.

  • The conditional compilation is controlled by UNITY_ASSERTION define.

  • Defines DEBUG and TRACE are now also defined for non-developer’s
    builds.

  • A wrapping library providing extension methods is also available
    under UnityEngine.Assertions.Must
    namespace.
    Source

TL;DR: check out UnityEngine.Assertions

No, but it's very easy to make your own:

static function Assert(condition : boolean)
{
    if (!condition) throw new Exception();
}

Though much better to make your own custom exception for it

If you want it to be stripped for build, you'll need to use c#:

#define DEBUG

using System;

public class DebugUtils
{
    static void Assert(bool condition)
    {
        #if DEBUG
        if (!condition) throw new Exception();
        #endif
    }
}

The correct answer to this as of 5.1 is now:

Assert.IsTrue(someCondition, "Assertion failed :( :(");

There are a couple more static functions under Assert for boolean and floating point comparisons.

It's not an assert, but if you use Debug.LogWarning/LogError, you can tell the game to pause in the editor on the frame that print statement was issued by using the "error pause" button on the console. You can then click on those errors to see a call stack. For released games it'll just print that message to the player log.

There seems to be a Debug.Assert function now in the Unity namespace, which seems like the ideal solution:

Debug.Assert is a function provided by the UnityEngine library.

For anyone else that comes across this thread… this is something that I was missing as well when we started working with Unity, so I wrote my own solution. If you’re interested, I have released my Unity debugging tools as a plugin, ProDebug, which has a number of useful tools that you might want such as:

  • Assert methods with customizable behaviors
  • Drop-in replacement for all UnityEngine.Debug class methods, allowing custom behaviors and MUCH faster logging performance (especially on mobile devices)
  • Hierarchical debug menu system
  • C# debug command executor (can be used as a runtime debug command console)

Happy debugging :O)

C# has a built in Assert method:

Without any using statements, it would look something like this:

System.Diagnostics.Debug.Assert(my_value != invalid_value, "my_value is invalid! my_value  = " + my_value);