docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Send events with RPCs and custom messages

    Send one-off events between the server and its clients with GhostObjects, using RPC methods and custom messages.

    A GhostField replicates ongoing state, and it's eventually consistent. A client receives the latest value, but not necessarily every intermediate one. Use a remote procedure call (RPC) when you need the opposite guarantee: for a one-off event that must arrive exactly once, such as a chat message, a scoreboard update, or a round-starting announcement.

    For an introduction to GhostObjects and their building blocks, refer to Introduction to GhostObjects.

    Choose how to declare an RPC

    There are two ways to declare an RPC, and they suit different cases:

    Approach Use it when
    An RPC method You want to call a method on one machine and have it run on another. This is the easiest path for most events.
    A custom message You want to send a data payload and handle it somewhere other than where you sent it, or collect several received messages and process them together.

    Declare an RPC method

    Add the RPC attribute to a method, and pass a SendDirection value to say which way the call travels. Calling the method then runs its body on the receiving machine instead of locally.

    You can put the attribute on a static method or on a method of a GhostBehaviour. Declare the class that holds the method as partial, because Netcode generates code in the other half of the class:

    public partial class Announcer
    {
        // Runs on every connected client when the server calls it.
        [RPC(SendDirection.ServerToClient)]
        public static void AnnounceRoundStart(int roundNumber)
        {
            Debug.Log($"Round {roundNumber} is starting.");
        }
    }
    
    public partial class PlayerActions : GhostBehaviour
    {
        // Runs on the server, on the server's copy of this same ghost.
        [RPC(SendDirection.ClientToServer)]
        public void RequestEmote(int emoteId)
        {
            Debug.Log($"Player {Ghost.OwnerNetworkId.Value} played emote {emoteId}.");
        }
    }
    

    To send either one, call it like any other method:

    Announcer.AnnounceRoundStart(3);
    

    Netcode rewrites the method so that calling it sends the arguments to the remote machine, where the body you wrote runs at the start of a frame, before Update. An RPC method on a GhostBehaviour targets the matching ghost on the receiving side, so RequestEmote runs on the server's copy of the same ghost, with the same GhostBehaviour type.

    Keep the following in mind:

    • You must pass a SendDirection. An RPC method without one doesn't compile.
    • Calling a ClientToServer RPC method on the server, or a ServerToClient RPC method on a client, logs an error.
    • Parameters can use only types that Netcode can serialize, such as numbers, bool, enums, Unity.Mathematics types, fixed-size strings, and structs made of these. You can't send string, arrays, lists, classes, or references to objects such as a GameObject. The same rule applies to the fields of a custom message.
    • A ServerToClient RPC method goes to every connected client.

    Declare a custom message

    Declare a struct that implements ICustomMessage, with the data you want to send as its fields:

    public struct ScoreUpdate : ICustomMessage
    {
        public int PlayerNetworkId;
        public int Score;
    }
    

    Applying the CustomMessage attribute to a partial struct implements ICustomMessage for you, so the following is equivalent:

    [CustomMessage]
    public partial struct ScoreUpdateFromAttribute
    {
        public int PlayerNetworkId;
        public int Score;
    }
    

    Send a custom message

    Call Netcode.RPC.Invoke, passing the value and the target that receives it. Every overload takes either a Connection (or a list of connections), or a World plus the SendDirection of the message.

    To send to one client from the server:

    Netcode.RPC.Invoke(new ScoreUpdate { PlayerNetworkId = 2, Score = 40 }, connection);
    

    To send to every connected client from the server, pass Netcode.AllConnections:

    Netcode.RPC.Invoke(new ScoreUpdate { PlayerNetworkId = 2, Score = 40 }, Netcode.AllConnections);
    

    Sending to a Connection is the only way to target an individual client.

    Poll for received messages

    There are two ways to receive a custom message: poll for it, or handle it automatically. Pick one per type, because they're mutually exclusive.

    To poll for received messages, call Netcode.RPC.Query. It returns every message of that type that arrived after your last call for that type, and clears them:

    foreach (ScoreUpdate update in Netcode.RPC.Query<ScoreUpdate>())
    {
        Debug.Log($"Player {update.PlayerNetworkId} now has {update.Score} points.");
    }
    

    Call Query regularly, for example from Update, or received messages accumulate. Passing no argument queries every netcode world. On a single-world host there's only one world, which acts as both the client and the server. Pass a World or a Connection to narrow the scope. Passing a Connection also tells you which client sent a message:

    foreach (Connection connection in Netcode.AllConnections)
    {
        foreach (ScoreUpdate update in Netcode.RPC.Query<ScoreUpdate>(connection))
        {
            Debug.Log($"{connection.NetworkId.Value} reported {update.Score}.");
        }
    }
    

    Handle received messages automatically

    To handle received messages automatically, add a Handle method to the struct. Netcode calls it on the receiving machine as soon as the message arrives, and the message is then consumed, so Query never returns it:

    public partial struct ScoreUpdateFromCustomMessage : ICustomMessage
    {
        public int PlayerNetworkId;
        public int Score;
    
        void Handle()
        {
            Debug.Log($"Player {PlayerNetworkId} now has {Score} points.");
        }
    }
    

    A Handle method takes no parameters. Netcode calls it at the start of a frame, before Update runs.

    Additional resources

    • Introduction to GhostObjects
    • Write prediction logic in a GhostBehaviour
    • Manage connections
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)