docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    RPC vs NetworkVariable

    Choosing the wrong data syncing mechanism can create bugs, use too much bandwidth, and add too much complexity to your code. Netcode for GameObjects (Netcode) has two main ways of syncing information between players: RPCs (Remote Procedure Calls) and replicated states (NetworkVariables). They both send messages over the network. The logic and your design around how they send messages is what will make you choose one over the other.

    Choosing between NetworkVariables or RPCs

    • Use RPCs for transient events, information only useful for a moment when it's received.
    • Use NetworkVariables for persistent states, for information that will be around more than a moment.

    A quick way to choose which to use is to ask yourself: "Should a player joining mid-game get that information?"

    Network Variables allow to seamlessly catch up late joining clients by sending the current state as soon as the tick happens.

    Using the Boss Room's door as an example. A player's client needs to receive the information that the door is open to play the right animations.

    If we sent an RPC to all clients, then all players connecting mid-game after that RPC is sent will miss that information and have the wrong visual on their clients.

    Sending state with RPCs won't be transmitted to late joining clients.

    NetworkVariables are eventually consistent. This means not all value changes will be synced, contrary to RPCs, where five calls to an RPC will produce five RPC sends on the network.

    Network Variables can be updated multiple times between ticks, but only the latest will be synced to other peers.

    NetworkVariables will save on bandwidth for you, making sure to only send values when the data has changed. However, if you want all value changes, RPCs might be best.

    Why not use NetworkVariables for everything?

    RPCs are simpler.

    If you have a temporary event like an explosion, you don't need a replicated state for this. It would not make sense. You would have an "unexploded" state that would need to be synced every time a new player connected? From a design perspective, you might not want to represent these events as state.

    An explosion can use an RPC for the event, but the effect of the explosion should be using NetworkVariables (for example player's knockback and health decrease). A newly connected player doesn't care about an explosion that happened five seconds ago. They do care about the current health of the players around that explosion though.

    If you want to make sure two variables are received at the same time, RPCs are great for this.

    If you change NetworkVariables "a" and "b", there is no guarantee they will both be received client side at the same time.

    Different Network Variables updated within the same tick aren't guaranteed to be delivered to the clients at the same time.

    Sending them as two parameters in the same RPC ensures they will be received at the same time client side.

    To ensure that several different Network Variables are all synchronized at the same exact time we can use client RPC to join these value changes together.

    NetworkVariables are great when you only care about the latest value.

    Summary

    NetworkVariables are great for managing state, to make sure everyone has the latest value. Use them when you want to make sure newly connected players get an up to date world state.

    RPCs are great for sending transient events. Use them when transmitting short-lived events.

    In This Article
    Back to top
    Copyright © 2025 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)