The code below should be pretty straightforward.
I am also using the basic networking HUD to set things up. I am running an instance of the game from the editor and selecting LAN Host, and when run a build of the game and connect select LAN Client the OnServerConnect function is called and the host game then starts the game, perfect, however the client does nothing.
OnServerConnect is obviously not called by the client, and because the RPC isn’t working that’s why nothing happens, so how do I make a Remote Procedure Call with UNet?
I know previously you’d need a Network View component and use that but I’ve seen people post examples similar to this one where you just label the function as a ClientRpc so what am I missing?
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class NetworkController : NetworkManager
{
// called when a client connects
public override void OnServerConnect(NetworkConnection conn)
{
StartGame();
}
[ClientRpc]
void StartGame()
{
UIController.Instance.StartGame();
}
}
All the UNet attributes only work on NetworkBehaviour, if you look into NetworkManager you will notice it does inherit just a standard MonoBehaviour. That’s why it doesn’t work.
Also all [ClientRpc] methods need to start with Rpc prefix.
Will compile but won’t work, UNet won’t even consider using it since it is not a NetworkBehaviour:
public class Class : MonoBehaviour
{
[ClientRpc]
public void Method()
{
}
}
Will not compile: UNetWeaver error: Rpc function [Class: Method] doesnt have ‘Rpc’ prefix:
public class Class : NetworkBehaviour
{
[ClientRpc]
public void Method()
{
}
}