- Home /
UNet ClientRPC does not fire
Unity 5.2.3f1 Windows 10 C#
I am basically making a multiplayer board game. Players need to have a stack of tiles they can play, move, etc. that all have independant stats. I have set up a player network object with the NetworkBehaviour shown below. ass you can see most of my functions are structured fairly similarly, but I am getting VERY different results.
SceneChange and MoveTile work perfectly one bothe the client and server, as well as FRROM the client and server. StackDeck and SyncTile are another matter. Both of them execute the Command only. By this I don't mean that the code doesn't work right for the ClientRpc call, I mean the call doesn't happen. Anywhere. Client or server, Unity editor or built project, developer build or not, NOWHERE does even the debug call give output from those two RPC calls, nor are errors thrown.
I am at a loss. I have been changing things back and forth trying to find a cause, or even just what makes the two functions that work different, but I've come up short. Any assistance would be greatly appriciated.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using TTools;
public class TilesNetworkManager : NetworkBehaviour
{
public static TilesNetworkManager instance;
public static int netLock = 0;
private NetworkManager netManager;
void Awake()
{
DontDestroyOnLoad( gameObject );
}
void Start()
{
instance = this;
netManager = GameController.Instance.netManager;
}
public static void SceneChange( string scene )
{
instance.CmdSceneChange( scene );
}
public static void StackDeck( TileDeck savedDeck )
{
netLock++;
instance.CmdStackDeck( true, savedDeck.ToString() );
}
public static void MoveTile( Tile tile, BoardSlot slot )
{
instance.CmdMoveTile( tile.gameObject, slot.name );
}
public static void SyncTile( GameObject tile )
{
instance.CmdSyncTile( tile );
}
[Command]
public void CmdSceneChange( string scene )
{
netManager.ServerChangeScene( scene );
}
[Command]
public void CmdStackDeck( bool playerA, string deckString )
{
if( Board.Instance.deckA.Count > 0 )
{
Debug.Log("Deck B");
TileDeck savedDeck = new TileDeck();
savedDeck.FromString( deckString );
Board.Instance.StackDeck( false, savedDeck );
}
else
{
Debug.Log("Deck A");
TileDeck savedDeck = new TileDeck();
savedDeck.FromString( deckString );
Board.Instance.StackDeck( true, savedDeck );
}
RpcStackDeck(false,deckString);
netLock--;
}
[Command]
public void CmdMoveTile( GameObject tile, string slot )
{
Debug.Log("Server Move");
RpcMoveTile( tile, slot );
}
[Command]
public void CmdSyncTile( GameObject tile )
{
Debug.Log("Server Load Tile");
NetworkIdentity objNetId = tile.GetComponent<NetworkIdentity> (); // get the object's network ID
objNetId.AssignClientAuthority (connectionToClient); // assign authority to the player who is changing the color
RpcSyncTile( tile ); // usse a Client RPC function to "paint" the object on all clients
objNetId.RemoveClientAuthority (connectionToClient); // remove the authority from the player who changed the color
//GetComponent<Tile>().LoadTile( Board.Instance.roster[ID], ID );
}
[ClientRpc]
public void RpcStackDeck( bool playerA, string deckString )
{
Debug.Log("Deck B made here");
TileDeck savedDeck = new TileDeck();
savedDeck.FromString( deckString );
Board.Instance.StackDeck( false, savedDeck );
netLock--;
}
[ClientRpc]
public void RpcMoveTile( GameObject tile, string slot )
{
Debug.Log("Client Move");
Tool.StartCoroutine( Board.Instance.MoveTile( tile.GetComponent<Tile>(), Board.FindSlot(slot) ) );
}
[ClientRpc]
public void RpcSyncTile( GameObject tile )
{
Debug.Log("Client Load Tile");
int id = tile.GetComponent<TileNet>().ID;
tile.GetComponent<Tile>().LoadTile( Board.Instance.roster[id], id );
}
}
Answer by JohnBubriski · Aug 25, 2016 at 02:45 PM
See the answer here: http://answers.unity3d.com/questions/1180855/why-is-this-clientrpc-not-executed.html
"I was calling the ClientRpc method before the game player objects on clients were properly set up by OnLobbyServerSceneLoadedForPlayer. To fix it I just waited with calling the ClientRpc until after the start method was invoked on all client gameplayer objects."
I confirmed that something like this is the problem/solution. In my case I used OnStartLocalPlayer() in my player controller to call a Command method on the server, which in turn called an Rpc method on the clients. Trying to call Rpc methods in other ways resulted in nothing happening, probably because there were no players ready, including the local one!
To be more specific, I'm creating a multiplayer game where the map is procedurally generated by the server each session.
Player code snippet:
public override void OnStartLocalPlayer()
{
_buildingManager = FindObjectOfType<BuildingManager>();
_buildingManager.CmdPlayerReady();
}
BuildingManager code snippet (Thing that sets up the map):
[Command]
public void CmdPlayerReady()
{
RpcSetupMap();
}
[ClientRpc]
public void RpcSetupMap()
{
Debug.Log("Spawn Blocks.");
SpawnBlocks();
Debug.Log("Spawn Buildings.");
SpawnBuildings();
}
In case anybody asks, I'm also using Random.InitState(Seed);
to generate the same map on each client. Seed
is a [SyncVar ]
that gets randomly generated by the server each session.
I'm on version 5.4.0f3 Personal.
Building$$anonymous$$anager is sever authority?if it is ,how it can use command function?
Answer by nsmith1024 · Jul 05, 2016 at 07:03 PM
I have the same problem in the latest version of Unity 5.4.0b23
[Command] works but not [clientRpc], same goes for the previous Unity release.
I cant understand how everybody is not seeing this huge glaring bug. The networking rpc calls are just not working, but strange nobody seems to care. But i cant do any work because of this right now since my game uses network heavily and networking clientRpc functions dont work at all.
Is Unity still in business or are they closed down?
Your answer
Follow this Question
Related Questions
ClientRpc Function not being carried out on all clients in Unity3d c# 0 Answers
Qos type ReliableFragmented channel, won't fragment our stream 2 Answers
Unet: Client/Server end up with two different animations 0 Answers
How to synchronize disable and enabling game objects over the network? 0 Answers
Destroying entire parent object on one client but on the other destroying just the child. 1 Answer