ClientRpc not called on Client

Hi all,

I’m hoping for any help on this one. I have some code that is called on a NetworkServer.Spawn()'ed object:

class InitClass : NetworkBehaviour
    [ClientRpc] public void RpcInitialize() { print("initialized"); }

    override public void OnStartServer() { RpcInitialize(); }

This method does not get called when I run the client in editor. But it does get called when I host from editor.

I have tried setting the object to server only but still nothing.

Any help is appreciated.


Well your setup doesn’t make much sense. OnStartServer is called when the server is initialized / started up. At this point no client can be connected. So the rpc won’t reach anyone. RPC calls are not buffered. So when a client actually joins the server the object get created on the client but of course it won’t receive the RPC unless you send it again.

You usually use the OnStartClient callback to do any initialization on the client side. However if you keep your relevant state in SyncVars you don’t have to do anything. If a new client connects to an already running server all tracked network objects will be spawned automatically on the client and they have their syncvars updated to the latest state from the server. OnStartClient will be called afterwards.