Heya, I’m fairly new to multiplayer dev. I come here because I’ve scoured the forums and documentation myself and cannot find a solution. I’ve got a ServerRpc that just… won’t run. I’ve ensured it’s being called on a spawned NetworkBehaviour, with a NetworkObject present. Their NetworkObjectIds are consistent both on host and client, and the code runs just fine when called by the host. The behaviour I’m seeing is consistent with hosting from both the build and in-editor, and I’m quite frankly stumpted at this point.
I’m using Lobby and Relay to join clients via a secure p2p connection.
After a host is started, it calls this ServerRpc. It runs as intended, and adds the player’s details to a list on the object.
[ServerRpc(RequireOwnership = false)]
public void OnPlayerJoinedServerRpc(string addition)
{
Debug.Log("OPJSRPC" + addition);
lobby.connectedPlayers.Add(JsonConvert.DeserializeObject<ConnectedPlayer>(addition));
string playerList = JsonConvert.SerializeObject(lobby.connectedPlayers);
UpdatePlayerListClientRpc(playerList);
}
Everything works fine, and the player’s name appears in my lobby’s connectedPlayers display. When a client is started, it starts this coroutine (to ensure the client has fully initialized before continuing - the same behaviour is seen when directly running the ServerRpc.
IEnumerator OnPlayerJoinedWait()
{
yield return new WaitUntil(() => NetworkManager.LocalClient != null);
Debug.Log("Client Online, running OPJSRPC");
string json = JsonConvert.SerializeObject(new ConnectedPlayer()
{
clientID = NetworkManager.Singleton.LocalClientId,
playerID = AuthenticationService.Instance.PlayerId,
playerName = AuthenticationService.Instance.PlayerName,
team = PlayerTeam.None
}, Formatting.Indented);
Debug.Log("Converted ConnectedPlayer to json object:\n" + json);
OnPlayerJoinedServerRpc(json);
Debug.Log("Called OPJSRPC");
}
On the host, I recieve the following debug logs:
(start host)
“OPJSRPC [playerjson]”
“UPLCRPC” (a log confirming UpdatePlayerListClientRpc was called)
(start client)
(nothing)
On the client, I recieve the following debug logs:
(start client)
“Client Online, running OPJSRPC”
“Converted ConnectedPlayer to json object: [playerjson]”
“Called OPJSRPC”
The host does not recieve the rpc call, and the client is not given the returning ClientRpc. I have confirmed the client and host are correctly connected, as attempting to load a scene with NetworkManager.SceneManager.LoadScene("Gameplay", UnityEngine.SceneManagement.LoadSceneMode.Single);
correctly loads the called scene. Any help would be greatly appreciated. I’m worried I’m going to have to do something hacky and put the RPC calls on a separate NetworkBehaviour on a prefab and spawn it when the host is created, but that sounds pretty jank.