Visual Scripting: How to take advantage of Custom Events

This time, we will introduce how to use Custom Event in Visual Scripting.

What is Custom Event?

In Unity’s Visual Scripting, a Custom Event is a mechanism that allows you to define and call custom events at any time. It is useful for communication between scripts and triggering specific processes.

This time, we will create a system where pressing a button changes the color of an object.

We will create a Script Graph with this structure.

Image2

The completed project can be downloaded from the following:

GitHub Repository

Preparation

Place the following objects in the scene:

  • Cube (the object whose color will change)
  • Button × 2 (buttons labeled “Red” and “Blue”)

Arrange the objects appropriately (refer to the sample layout).

Creating ChangeColorReceiver

1. Add a “Script Machine” component to the Cube.

2. Create a new Script Graph and name it ChangeColorReceiver.

3. Create String-type variables named “Change Red” and “Change Blue”.

Image3

Add the following nodes:

  • Custom Event: Set an event name and configure it to receive a String-type argument.
  • Get Material: Retrieve the Cube’s material.
  • Change Color: Change the material color based on the received argument.
Image8

Creating ChangeBlueGraph and ChangeRedGraph

1. Add a “Script Machine” component to each button.

2. Create the following Script Graphs for each button:

  • For Red: ChangeRedGraph
  • For Blue: ChangeBlueGraph

3. Create a GameObject-type variable named “ChangeColorCube” and assign the Cube to it.

Image1
Image7

Add the following nodes:

On Button Click: Starts the process when the button is clicked.

Call Custom Event: Input the String-type variable names “ChangeRed” and “ChangeBlue” from the ChangeColorReceiver creation step into the CustomEvent node (make sure to enter the spelling correctly).

Image4
Image6

Execution

Pressing the buttons will change the Cube’s color to either “red” or “blue”.

Conclusion

Custom Events are a useful mechanism in Visual Scripting that enable flexible communication between scripts. By leveraging this system, you can build simple and manageable event-driven systems.