Debug.Log Extensions
Documentation (1.1.9)

◆ Guard< TException >()

static void Debug.Guard< TException > ( bool  condition,
params object[]  exceptionArguments 
)
static

If condition is false throws an exception of type TException .

If condition is true does nothing.

This can be useful for checking that the arguments passed to a function are valid and if not terminating the process with an error.

Note that an exception will be thrown every time that this method is called and condition evaluates to false. This is in contrast to some of the other Guard methods that only log an error once per session.

Calls to this method will be full stripped in release builds if build stripping has been enabled in preferences. If you don't want this behaviour you can use Critical.Guard<TException>(bool, object[]) instead.

private void CopyComponent(Component component, GameObject to)
{
Debug.Guard<ArgumentNullException>(component != null, nameof(component)));
Debug.Guard<ArgumentNullException>(to != null, nameof(to));
var copy = to.AddComponent(component.GetType());
var json = JsonUtility.ToJson(component);
JsonUtility.FromJsonOverwrite(json, copy);
}
static bool Guard(bool condition, Object context=null)
If condition is false logs to the Console an error message and returns true.
Definition: Debug.cs:4102
Extended version of the built-in Debug class with additional methods to ease debugging while developi...
Definition: Debug.cs:34
Parameters
conditionCondition you expect to be true.
exceptionArgumentsOptional arguments to pass to the TException constructor when an exception is thrown.

Most exception constructors accept a string argument specifying the message to be logged to the Console. For ArgumentNullException on the other hand a single string argument specifies the name of the parameter that was null.

Returns
true if condition was false; otherwise, false.
Type Constraints
TException :Exception 
TException :new()