Parameters

Parameter types

All primitive types and basic Unity value types such as Vector3 and Color are supported out of the box. Also, most types derived from UnityEngine.Object can be used as parameters.

This means types like GameObject, ScriptableObject, MonoBehaviour can be used as parameters without any setup. When typing the parameter in the command field, we specify the name of the object we want to reference. The object is then found automatically with the given name.

It's also possible to use prefab game objects as parameters. To do this, we can place a [Prefab] attribute in front of our parameter. Like so:

[Command]
public string SetPrefabTag([Prefab]GameObject prefab, string tag)
{
    if (prefab != null)
        prefab.tag = tag;
}

Optional parameters

[Command]
public void Damage(int amount = 1)
{
    health -= amount;
}

Optional parameter values are also supported. You can simply define a method with default optional parameters. Given the example above, if you were to execute the command without any argument, it would default to dealing 1 damage.

Last updated

Was this helpful?