Hi,
I have some issue with networking, especially with Remote Procedure Calls,
I simply want to call Command function and ClientRpc function but I have strange error.
When player connect to the server, the network instantiate a gameObject “Player” with a NetworkView and a NetworkIdentity (Local Player Authority checked).
If the player is the host and want to increment his “number”, the script must call RpcactualiseNumberClient but the function is never called and the player receive the error “RPC Function RpcactualiseNumberClient called on client”.
If the player is not the host but just a client and want to increment his “number”, the script must call CmdactualiseNumberServer but this function is never called and the player receive the error “Command function CmdactualiseNumberServer call on server”. What is wrong ?
There is the Player script :
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Player : NetworkBehaviour {
public int number;
// Use this for initialization
void Start () {
number = 0;
}
// Update is called once per frame
void Update () {
if(GetComponent<NetworkView>().isMine){
if(Input.GetKeyDown(KeyCode.Space)){
if(!Network.isServer)
CmdactualiseNumberServer(number+1);
else
RpcactualiseNumberClient(number+1);
}
}
}
[Command]
void CmdactualiseNumberServer(int num){
number = num;
RpcactualiseNumberClient(number);
}
[ClientRpc]
void RpcactualiseNumberClient(int num){
if(GetComponent<NetworkView>().isMine)
number = num;
}
}
and there, the NetworkManager Script for create server and connections :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Network_Manager : MonoBehaviour {
public GameObject playerPrefab;
const string typeName = "NameOfTheGame";
const string gameName = "NomDuServer";
public Button bouton_start;
public Button bouton_refresh;
public Text liste_hotes;
public Text infos;
HostData[] hostList;
HostData hote_actuel;
// Use this for initialization
void Start () {
hostList = new HostData[0];
bouton_start.onClick.AddListener( () => {
StartServer();
});
bouton_refresh.onClick.AddListener(() => {
RefreshHostList();
});
}
// Update is called once per frame
void Update () {
afficherListeHotes();
infos.text = "";
foreach(Player player in GameObject.FindObjectsOfType<Player>()){
infos.text += player.name + " : " + player.number+"\n";
}
}
void StartServer() {
Network.InitializeServer(2,28059, !Network.HavePublicAddress());
MasterServer.RegisterHost(typeName,"Default");
}
void RefreshHostList(){
MasterServer.RequestHostList(typeName);
infos.text = "Refresh done.";
}
void OnPlayerConnected(NetworkPlayer player){
Debug.Log("Connected. " + Network.connections.Length);
infos.text = "Connected. " + Network.connections.Length;
}
void OnMasterServerEvent(MasterServerEvent msEvent){
if(msEvent==MasterServerEvent.HostListReceived){
hostList = MasterServer.PollHostList();
infos.text = "host list received. "+hostList.Length;
}
if(msEvent==MasterServerEvent.RegistrationSucceeded){
Debug.Log("registration succeededs");
infos.text = "registration succeded:";
}
}
void OnServerInitialized(){
Debug.Log("Server initialized. IP : " + MasterServer.ipAddress);
Network.Instantiate(playerPrefab,Vector3.zero, Quaternion.identity,0);
}
void JoinServer(HostData hostData){
Network.Connect(hostData);
hote_actuel = hostData;
Debug.Log("Connection.");
infos.text = "Connection.";
}
void OnConnectedToServer(){
Debug.Log("Server joined.");
infos.text = "Serveur joined.";
Network.Instantiate(playerPrefab,Vector3.zero,Quaternion.identity,0);
}
void afficherListeHotes(){
string txt="";
if(hostList!=null){
for(int i=0;i<hostList.Length;++i){
txt+=hostList[i].gameName+" " + hostList[i].connectedPlayers + "\n";
}
}
liste_hotes.text = txt;
}
void OnGUI(){
if(hostList!=null) {
for(int i=0;i<hostList.Length;++i) {
if(GUI.Button(new Rect(100,0+50*i,100,40),hostList[i].gameName)) {
JoinServer(hostList[i]);
}
}
}
}
}
I use Unity Personal 5.1.1f1 (64-bit)

