- Home /
Networking RPC sends to wrong target
I am making a card game. The host player or "server" has a Menu Object and _Main1 in their scene. The other player or "client" also has a unique Menu Object but a _Main2 in their scene. As a child for each _Main object there is a Player object and under that a Deck, Hand, Field, Graveyard. E.g. _Main1>Player1>Deck,Hand,Field,Graveyard & _Main2>Player2>Deck,Hand,Field,Graveyard. In both of the _Main objects there are the following integer variables: NoOfCards and NoOfCards2. For the Server NoOfCards is set to the amount of children In the "Hand" object which contains objects named "Cards".
So when the game is first setup in _Main1, NoOfCards is set to 5. Now it needs to find the value of NoOfCards2. The way to do this I believe is get the value straight from _Main2 on the "client". To do this I use RPCs. Below is the section of code which tries to find the values of NoOfCards(2), the _Main1 and _Main2 and instantiated from one prefab and so I have the if(host) function to check which section it should run.
if(host){
NoOfCards = Player_1.transform.FindChild("Hand").childCount;
NoOfFieldCards = Player_1.transform.FindChild("Field").childCount;
networkView.RPC("SendData", RPCMode.OthersBuffered, "NoOfCards2");
networkView.RPC("SendData", RPCMode.OthersBuffered, "NoOfFieldCards2");
print ("NoOfCards2: "+NoOfCards2+" NoOfFieldCards2: "+NoOfFieldCards2);(**)
}
else{
NoOfCards2 = Player_2.transform.FindChild("Hand").childCount;
NoOfFieldCards2 = Player_2.transform.FindChild("Field").childCount;
networkView.RPC("SendData", RPCMode.Server, "NoOfCards");
networkView.RPC("SendData", RPCMode.Server, "NoOfFieldCards");
print ("NoOfCards: "+NoOfCards+" NoOfFieldCards: "+NoOfFieldCards);
}
Here are the RPCs that are used in the code above. It is designed so that you call for a variable from another object on the "client" for example using a string for the variable name. It then finds that variable on the "client" and returns it using reflection.
[RPC]
void SendData(string DataName){
print (this.GetType().GetField("NoOfCards2"));(*)
print (host);
if(this.GetType().GetField(DataName).GetValue(this).GetType() == typeof(int)){
networkView.RPC("ReceiveDataInt",RPCMode.AllBuffered,DataName,this.GetType().GetField(DataName).GetValue(this).GetType());
}
}
[RPC]
void ReceiveDataInt(string RequestedDataName, int DataReceived){
this.GetType().GetField(RequestedDataName).SetValue(this, DataReceived);
}
However from the Server when it tries to print(*) it returns Null and therefore the rest of the code fails too. I get the following error: "NullReferenceException: Object reference not set to an instance of an object Main.SendData (System.String DataName)".
Just in case it's useful I also get the following result from this print(**first section of code): "NoOfCards2: 0 NoOfFieldCards2: 0 UnityEngine.MonoBehaviour:print(Object) Main:OnGUI()"
I believe the problem is my setup overall and to do with networkView.viewIDs on the NetworkView components on the Menu objects and the _Main objects but it is beyond my understanding now and need help.
Your answer
Follow this Question
Related Questions
Multiplayer Moving Bullet 1 Answer
How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers
RPC and multiplayer networking 2 Answers
How can i send a Player list to every Client 0 Answers
Acess server stuff by the client 0 Answers