【VRChat】Methods and Custom Events: Script Communication with SendCustomEvent

Created: 2025-12-19

Code reuse and inter-script communication methods. Learn method definitions, SendCustomEvent, SendCustomNetworkEvent, and delayed execution (SendCustomEventDelayedSeconds).

Overview

As gimmicks become more complex, writing all logic within single events like Start or Update becomes inefficient and reduces code readability. By creating methods (functions) that group processing by functionality and calling them as needed, you can organize code and improve reusability.

Furthermore, UdonSharp provides a mechanism called custom events for calling processing from one script to another. This allows you to build complex systems where multiple gimmicks work together.

This article covers everything from basic method definitions to SendCustomEvent for inter-script communication, and SendCustomNetworkEvent for sending events to all players over the network.

1. Method (Function) Basics

A method is a block that groups a series of processing together. Instead of writing the same processing multiple times, you define a method once and call it whenever needed.

Defining Methods

Methods are defined in the following format:

return_type method_name(parameters) { // Processing to execute }

  • Return Type: The data type of the value the method returns as a result. Use void if the method doesn't return a value.
  • Method Name: The name that identifies the method.
  • Parameters: Data passed to the method. Leave () empty if not needed.
using UdonSharp;
using UnityEngine;

public class MethodExample : UdonSharpBehaviour
{
    // The simplest method with no parameters and no return value
    private void PlaySound()
    {
        // Processing to play sound...
        Debug.Log("Sound played.");
    }

    // A method that takes two int parameters and outputs the calculation result to console
    private void AddAndLog(int a, int b)
    {
        int result = a + b;
        Debug.Log($"Calculation result: {result}");
    }

    // A method that takes two int parameters and returns their sum as an int
    private int GetSum(int a, int b)
    {
        return a + b;
    }
}

Calling Methods

Call defined methods in the format method_name(arguments);.

public class MethodCaller : UdonSharpBehaviour
{
    void Start()
    {
        // Call methods within the same script
        PlaySound(); // Displays "Sound played."

        AddAndLog(10, 5); // Displays "Calculation result: 15"

        int sum = GetSum(7, 3);
        Debug.Log($"GetSum result is {sum}."); // Displays "GetSum result is 10."
    }

    // (Method definitions above are omitted)
}

2. Inter-Script Communication: SendCustomEvent

SendCustomEvent is a way to call methods on other UdonSharpBehaviours. Using this mechanism, you can send an "event name" to an UdonSharpBehaviour attached to a target object and execute the public method with that name.

Note: In UdonSharp, if you have a reference to another UdonSharpBehaviour as a variable, you can also directly call its public methods. SendCustomEvent is particularly useful when you want to dynamically specify method names as strings or when you want to use delayed execution described later.

targetUdonBehaviour.SendCustomEvent("EventName");

  • Target UdonBehaviour: The Udon Behaviour component you want to send the event to.
  • Event Name: The name of the method to execute, specified as a string.

Implementation Example: Button and Door

Let's implement a linkage where clicking a "Button" object opens a "Door" object.

DoorController.cs (Attached to the door)

using UdonSharp;
using UnityEngine;

public class DoorController : UdonSharpBehaviour
{
    // This method is called as a custom event
    public void OpenDoor()
    {
        Debug.Log("Door is opening.");
        // Processing to play door opening animation, etc...
    }
}

ButtonController.cs (Attached to the button)

using UdonSharp;
using UnityEngine;

public class ButtonController : UdonSharpBehaviour
{
    // Assign the door's Udon Behaviour component in the Inspector
    public UdonSharpBehaviour doorUdonBehaviour;

    public override void Interact()
    {
        // Send a custom event named "OpenDoor" to doorUdonBehaviour
        doorUdonBehaviour.SendCustomEvent("OpenDoor");
    }
}

With this setup, when a player Interacts with the button, ButtonController sends the OpenDoor event to DoorController, resulting in the door opening processing being executed. This occurs only within your own client.

3. Sending Events to All Players: SendCustomNetworkEvent

While SendCustomEvent is used for local communication, SendCustomNetworkEvent sends custom events to all players in the instance. This allows you to execute specific processing simultaneously across all players' environments.

targetUdonBehaviour.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, "EventName");

  • NetworkEventTarget.All: Specifies to send to all players.

Implementation Example: Sound Playing for Everyone Simultaneously

A gimmick where when the game master presses a button, a "game start" sound plays on all players' clients.

SoundPlayer.cs (Responsible for playing sound on each player's client)

using UdonSharp;
using UnityEngine;

public class SoundPlayer : UdonSharpBehaviour
{
    public AudioSource startSound;

    // This method is called for all players by SendCustomNetworkEvent
    public void PlayStartSound()
    {
        startSound.Play();
    }
}

GameMasterButton.cs (Button pressed by the game master)

using UdonSharp;
using UnityEngine;
using VRC.Udon.Common.Interfaces;

public class GameMasterButton : UdonSharpBehaviour
{
    // Assign SoundPlayer's Udon Behaviour in the Inspector
    public UdonSharpBehaviour soundPlayerUdonBehaviour;

    public override void Interact()
    {
        // Send "PlayStartSound" event to all players
        soundPlayerUdonBehaviour.SendCustomNetworkEvent(NetworkEventTarget.All, "PlayStartSound");
    }
}

Important: SendCustomNetworkEvent only sends the "trigger for processing" to everyone. If you want to synchronize object state (whether a door is open, etc.), you need to use [UdonSynced] variables and serialization as learned in previous articles.

4. Delayed Events: SendCustomEventDelayedSeconds

To execute an event after a certain time, use SendCustomEventDelayedSeconds. This is a very important method for implementing timer processing since UdonSharp doesn't support coroutines.

// Execute "PlayExplosion" method after 3 seconds
SendCustomEventDelayedSeconds("PlayExplosion", 3.0f);

// Execute "CheckStatus" method after 5 frames
SendCustomEventDelayedFrames("CheckStatus", 5);
  • SendCustomEventDelayedSeconds("EventName", seconds): Executes the event after the specified number of seconds.
  • SendCustomEventDelayedFrames("EventName", frames): Executes the event after the specified number of frames.

These execute locally and can improve performance by replacing continuous flag checking in Update.

Comparison of Event Sending Methods

MethodExecution ScopeCommunicationMain Use
Direct CallSame scriptLocalInternal processing organization, code reuse
SendCustomEventSpecified objectLocalInter-script communication within same client
SendCustomEventDelayedSecondsSelfLocalTimer processing, delayed execution
SendCustomNetworkEventAll playersNetworkExecute same processing simultaneously for all players

Summary

  • Grouping processing into methods organizes code and makes it easier to reuse.
  • To call processing on other objects, use SendCustomEvent("EventName"). This executes locally.
  • To have all players execute the same processing, use SendCustomNetworkEvent(NetworkEventTarget.All, "EventName"). This executes over the network.
  • To execute processing after a certain time, use SendCustomEventDelayedSeconds. Essential for timer processing since UdonSharp doesn't support coroutines.
  • Local SendCustomEvent cannot directly pass arguments, so you need other mechanisms like synced variables for sharing values.

By mastering methods and custom events, you can link individual gimmicks together to build larger, more systematic systems. The next article will cover handling player-related events such as joining and leaving.