Debug.Log Extensions
Documentation (1.1.9)

◆ Guard() [8/10]

static bool Debug.Guard ( bool  condition,
[NotNull] Expression< Func< object >>  classMember,
Object  context = null 

If condition is false logs to the Console an error message containing the name and value of classMember class member and returns true.

If condition is true returns false without logging anything.

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

An error is only logged the first time during a session that the condition evaluates to false to avoid flooding the log file.

In release builds an error will only be logged if the UNITY_ASSERTIONS symbol is defined.

private void CopyComponent(Component component, GameObject to)
if(Debug.Guard(component != null, ()=>component) || Debug.Guard(to != null, ()=>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
conditionCondition you expect to be true.
classMemberExpression pointing to a class member whose name and value will be logged.
contextObject to which the assertion applies.
true if condition was false; otherwise, false.