C# for Unity: Coroutines & Multiplayer Networking

Lesson 5 of 7

Server & Client RPCs

An RPC (Remote Procedure Call) lets one machine trigger a method that runs on another. Netcode gives you two directions.

public class PlayerShooter : NetworkBehaviour
{
    [ServerRpc]
    void FireServerRpc()
    {
        // Runs on the server only, the server owns the truth.
        SpawnBullet();
        FireClientRpc();
    }

    [ClientRpc]
    void FireClientRpc()
    {
        // Runs on every client, e.g. to play a muzzle-flash effect.
        PlayMuzzleFlash();
    }

    void Update()
    {
        if (IsOwner && Input.GetButtonDown("Fire1"))
            FireServerRpc();
    }
}

The rules

  • [ServerRpc] methods are called by a client (usually the owner) but always execute on the server, name them with a ServerRpc suffix.
  • [ClientRpc] methods are called by the server and execute on every connected client, suffix ClientRpc.
  • By default a ServerRpc can only be called by the object's owner, set RequireOwnership = false to allow any client to call it.
  • Never trust a client to decide game outcomes, a client calls a ServerRpc to request an action, the server validates and applies it, then tells everyone via a ClientRpc.

📝 RPCs Quiz

Passing score: 70%
  1. 1.A method marked [ServerRpc] always executes on which machine?

  2. 2.Setting RequireOwnership = false on a ServerRpc lets ____ client call it, not just the owner.

  3. 3.A client should be trusted to directly decide game-changing outcomes for performance reasons.