Hello, I'm having a bit of trouble understanding RPC calls. At the start of my game, they are asked to give their self a username that will be used for that session and it stores it in a PlayerStats script that is on an empty game object. When a player joins a lobby(maximum of 2 players), their lobby name is set by the username that is in the PlayerStats script. Although, when they join it isn't syncing each others names.
My Lobby Handler -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LobbyHandler : MonoBehaviour {
public Text playerOneName;
public Text playerTwoName;
public bool isLobbyPlayerOne;
public bool isLobbyPlayerTwo;
public GameObject Scripts;
public string playerOneNameString;
public string playerTwoNameString;
void Awake () {
Scripts = GameObject.Find("_Scripts");
playerOneName.text = "";
playerTwoName.text = "";
}
public void SetLobbyStatsFunction () {
StartCoroutine(SetLobbyStats());
}
[RPC] IEnumerator SetLobbyStats () {
yield return new WaitForSeconds(1);
if (Scripts.GetComponent ().isPlayerOne == true) {
isLobbyPlayerOne = true;
NetworkViewID viewID = Network.AllocateViewID();
GetComponent().RPC("AssignPlayerOne", RPCMode.AllBuffered, viewID, nameToAssign);
isLobbyPlayerTwo = false;
UpdatePlayerNames();
} else {
isLobbyPlayerOne = false;
NetworkViewID viewID = Network.AllocateViewID();
GetComponent().RPC("AssignPlayerTwo", RPCMode.AllBuffered, viewID, nameToAssign);
isLobbyPlayerTwo = true;
UpdatePlayerNames();
}
}
[RPC] void UpdatePlayerNames() {
//later
}
[RPC] void AssignPlayerOne (NetworkViewID viewID, string nameToAssign) {
playerOneNameString = Scripts.GetComponent().Username;
playerOneName.text = Scripts.GetComponent().Username;
}
[RPC] void AssignPlayerTwo (NetworkViewID viewID, string nameToAssign) {
playerTwoNameString = Scripts.GetComponent().Username;
playerTwoName.text = Scripts.GetComponent().Username;
}
}
↧