- Home /
Multiplayer Usernames c#
I'm having a lot of trouble synchronizing a player's username to the other player. 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. I need to find a way to get the other player's username to store it a LobbyHandler string.
LobbyHandler script -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LobbyHandler : MonoBehaviour {
public string playerName;
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 = "<Loading>";
playerTwoName.text = "<Waiting>";
}
void Start () {
playerName = GameObject.Find("_Scripts").GetComponent<PlayerStats>().Username;
}
[RPC] void OnPlayerConnected(NetworkPlayer player) {
StartCoroutine(SetLobbyStats());
ConsoleLog.Instance.Log(playerName + " joined.");
}
public void SetLobbyStatsFunction () {
StartCoroutine(SetLobbyStats());
}
[RPC] IEnumerator SetLobbyStats () {
yield return new WaitForSeconds(1);
if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
isLobbyPlayerOne = true;
isLobbyPlayerTwo = false;
AssignPlayerOne();
UpdatePlayerNames();
} else {
isLobbyPlayerOne = false;
AssignPlayerTwo();
isLobbyPlayerTwo = true;
UpdatePlayerNames();
}
}
[RPC] void UpdatePlayerNames() {
//
}
[RPC] void AssignPlayerOne () {
playerOneNameString = Scripts.GetComponent<PlayerStats>().Username;
playerOneName.text = Scripts.GetComponent<PlayerStats>().Username;
}
[RPC] void AssignPlayerTwo () {
playerTwoNameString = Scripts.GetComponent<PlayerStats>().Username;
playerTwoName.text = Scripts.GetComponent<PlayerStats>().Username;
}
}
PlayerStats Script -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerStats : MonoBehaviour {
public string Username = "Unassigned";
public bool isPlayerOne = false;
public bool isPlayerTwo = false;
public void CheckUsername () {
if (Username == "") {
Username = "Unassigned";
}
}
}
Answer by Eluate · Mar 19, 2015 at 09:52 AM
When you are calling RPC methods, you should be using NetworkView.RPC.
networkView.RPC("AssignPlayerTwo", RPCMode.AllBuffered, viewID, transform.position);
You need to set your code up so that the RPC methods are being called by NetworkView.RPC.
I updated my script, but it sets player one and player two's names to the person who is playing. I mean like, player one sees player one's name as both player one and player two. Vice versa for player two. What did I do wrong?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LobbyHandler : $$anonymous$$onoBehaviour {
public string playerName;
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 = "<Loading>";
playerTwoName.text = "<Waiting>";
}
void Start () {
playerName = GameObject.Find("_Scripts").GetComponent<PlayerStats>().Username;
}
[RPC] void OnPlayerConnected(NetworkPlayer player) {
StartCoroutine(SetLobbyStats());
ConsoleLog.Instance.Log(playerName + " joined.");
}
public void SetLobbyStatsFunction () {
StartCoroutine(SetLobbyStats());
}
[RPC] IEnumerator SetLobbyStats () {
yield return new WaitForSeconds(1);
if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
isLobbyPlayerOne = true;
NetworkViewID viewID = Network.AllocateViewID();
GetComponent<NetworkView>().RPC("AssignPlayerOne", RPC$$anonymous$$ode.AllBuffered, viewID, transform.position);
isLobbyPlayerTwo = false;
UpdatePlayerNames();
} else {
isLobbyPlayerOne = false;
NetworkViewID viewID = Network.AllocateViewID();
GetComponent<NetworkView>().RPC("AssignPlayerTwo", RPC$$anonymous$$ode.AllBuffered, viewID, transform.position);
isLobbyPlayerTwo = true;
UpdatePlayerNames();
}
}
[RPC] void UpdatePlayerNames() {
//later
}
[RPC] void AssignPlayerOne (NetworkViewID viewID, Vector3 location) {
playerOneNameString = Scripts.GetComponent<PlayerStats>().Username;
playerOneName.text = Scripts.GetComponent<PlayerStats>().Username;
}
[RPC] void AssignPlayerTwo (NetworkViewID viewID, Vector3 location) {
playerTwoNameString = Scripts.GetComponent<PlayerStats>().Username;
playerTwoName.text = Scripts.GetComponent<PlayerStats>().Username;
}
}
You need to make it so that in the RPC call (since it runs on both clients) that the player you are assigning to includes the if statement to check whether he is player one or two.
I got rid of some of the unneeded stuff. The result is still the same. Perhaps there is something wrong with my Network $$anonymous$$anager?(at bottom of this comment)
Lobby Handler
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LobbyHandler : $$anonymous$$onoBehaviour {
public Text playerOneName;
public Text playerTwoName;
public GameObject Scripts;
public string playerOneNameString;
public string playerTwoNameString;
void Awake () {
Scripts = GameObject.Find("_Scripts");
playerOneName.text = "<Loading>";
playerTwoName.text = "<Waiting>";
}
[RPC] void OnPlayerConnected(NetworkPlayer player) {
NetworkViewID viewID = Network.AllocateViewID();
GetComponent<NetworkView>().RPC("SetLobbyStats", RPC$$anonymous$$ode.AllBuffered, viewID, transform.position);
}
public void SetLobbyStatsFunction () {
StartCoroutine(SetLobbyStats());
}
[RPC] IEnumerator SetLobbyStats () {
yield return new WaitForSeconds(1);
if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
playerOneNameString = Scripts.GetComponent<PlayerStats>().Username;
playerOneName.text = Scripts.GetComponent<PlayerStats>().Username;
} else {
playerTwoNameString = Scripts.GetComponent<PlayerStats>().Username;
playerTwoName.text = Scripts.GetComponent<PlayerStats>().Username;
}
}
}
Network $$anonymous$$anger
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Network$$anonymous$$anager : $$anonymous$$onoBehaviour {
public const string typeName = "102398471982374987";
public const string gameName = "RoomName";
public GameObject playerPrefab;
public GameObject $$anonymous$$ain$$anonymous$$enuCanvas;
public GameObject LobbyCanvas;
public GameObject StartupCanvas;
private HostData[] hostList;
void Awake () {
StartupCanvas.SetActive(true);
}
void StartServer () {
Network.InitializeServer(2, 25000, !Network.HavePublicAddress());
$$anonymous$$asterServer.RegisterHost(typeName, gameName);
}
void OnServerInitialized()
{
SpawnPlayer();
GetComponent<LobbyHandler>().SetLobbyStatsFunction();
}
private void RefreshHostList()
{
$$anonymous$$asterServer.RequestHostList(typeName);
}
void On$$anonymous$$asterServerEvent($$anonymous$$asterServerEvent msEvent)
{
if (msEvent == $$anonymous$$asterServerEvent.HostListReceived)
hostList = $$anonymous$$asterServer.PollHostList();
}
private void JoinServer(HostData hostData)
{
Network.Connect(hostData);
$$anonymous$$ain$$anonymous$$enuCanvas.SetActive(false);
LobbyCanvas.SetActive(true);
GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerOne = false;
GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerTwo = true;
GetComponent<LobbyHandler>().SetLobbyStatsFunction();
}
void OnConnectedToServer()
{
SpawnPlayer();
}
private void SpawnPlayer()
{
Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
}
public void CreateButtonFunction() {
StartServer();
LobbyCanvas.SetActive(true);
$$anonymous$$ain$$anonymous$$enuCanvas.SetActive(false);
GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerOne = true;
GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerTwo = false;
}
public void JoinButtonFunction() {
RefreshHostList();
}
void OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
JoinServer(hostList[i]);
}
}
}
}
}
You are using RPC calls incorrectly. The RPC call is called on all other clients so the line
if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
does nothing.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Desync between host and client. 0 Answers
Show Photon username based on Distance 0 Answers
Understanding Network.Instantiate 1 Answer