YarnCommandAttribute Class

An attribute that marks a method on a MonoBehaviour as a command.

[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class YarnCommandAttribute : Attribute


When a DialogueRunner receives a Command, and no command handler has been installed for the command, it splits it by spaces, and then checks to see if the second word, if any, is the name of any GameObjects in the scene.

If one is found, it is checked to see if any of the MonoBehaviours attached to the class has a YarnCommandAttribute whose CommandString matching the first word of the command.

If a method is found, its parameters are checked:

YarnCommands and Coroutines

This attribute may be attached to a coroutine.

If the method is a coroutine, the DialogueRunner will pause execution until the coroutine ends.


The following C# code uses the YarnCommand attribute to register commands.

class ExampleBehaviour : MonoBehaviour {
   void Jump()
       Debug.Log($"{this.gameObject.name} is jumping!");

   void WalkToDestination(string destination) {
       Debug.Log($"{this.gameObject.name} is walking to {destination}!");

   IEnumerator ShineFlashlight(string durationString) {
       float.TryParse(durationString, out var duration);
       Debug.Log($"{this.gameObject.name} is turning on the flashlight for {duration} seconds!");
       yield new WaitForSeconds(duration);
       Debug.Log($"{this.gameObject.name} is turning off the flashlight!");

Next, assume that this ExampleBehaviour script has been attached to a GameObject present in the scene named “Mae”. The Jump and WalkToDestination methods may then be called from a Yarn script like so:

// Call the Jump() method in the ExampleBehaviour on Mae
<<jump Mae>>

// Call the WalkToDestination() method in the ExampleBehaviour 
// on Mae, passing "targetPoint" as a parameter
<<walk Mae targetPoint>>

// Call the ShineFlashlight method, passing "0.5" as a parameter;
// dialogue will wait until the coroutine ends.
<<shine_flashlight Mae 0.5>>

Running this Yarn code will result in the following text being logged to the Console:

Mae is jumping! 
Mae is walking to targetPoint! 
Mae is turning on the flashlight for 0.5 seconds!
(... 0.5 seconds elapse ...)
Mae is turning off the flashlight!


Name Description
CommandString The name of the command, as it exists in Yarn.


Defined in Unity/Assets/YarnSpinner/Scripts/DialogueRunner.cs, line 1047.