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.
![alt text][1]
[1]: http://img.prntscr.com/img?url=http://i.imgur.com/EzPQ6Ld.png
----------
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 = "";
playerTwoName.text = "";
}
void Start () {
playerName = GameObject.Find("_Scripts").GetComponent().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 ().isPlayerOne == true) {
isLobbyPlayerOne = true;
isLobbyPlayerTwo = false;
AssignPlayerOne();
UpdatePlayerNames();
} else {
isLobbyPlayerOne = false;
AssignPlayerTwo();
isLobbyPlayerTwo = true;
UpdatePlayerNames();
}
}
[RPC] void UpdatePlayerNames() {
//
}
[RPC] void AssignPlayerOne () {
playerOneNameString = Scripts.GetComponent().Username;
playerOneName.text = Scripts.GetComponent().Username;
}
[RPC] void AssignPlayerTwo () {
playerTwoNameString = Scripts.GetComponent().Username;
playerTwoName.text = Scripts.GetComponent().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";
}
}
}
↧