Hello, I'm having some trouble with nicknames. I am trying to make a 2 player game. On startup, the player is prompted asking for a username to use for the session, the string is then stored on an empty gameobject to be used for later. How would I go about getting the OTHER player's name? Meaning, if you were player one, how could you get player two's name stored in a string for use?
My script so far -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LobbyHandler : MonoBehaviour {
public Text playerOneName;
public Text playerTwoName;
public GameObject Scripts;
public string playerOneNameString;
public string playerTwoNameString;
NetworkViewID viewID;
public string myName;
void Awake () {
Scripts = GameObject.Find("_Scripts");
playerOneName.text = "";
playerTwoName.text = "";
}
void OnPlayerConnected() {
SetLobbyStatsFunction();
}
public void SetLobbyStatsFunction () {
myName = Scripts.GetComponent().Username;
StartCoroutine (SetLobbyStats());
}
IEnumerator SetLobbyStats () {
yield return new WaitForSeconds(1);
if (Scripts.GetComponent ().isPlayerOne == true) {
GetComponent().RPC("AssignPlayerOne", RPCMode.AllBuffered, viewID, myName);
} else {
GetComponent().RPC("AssignPlayerTwo", RPCMode.AllBuffered, viewID, myName);
}
}
[RPC]
void AssignPlayerOne(NetworkViewID viewID, string myName) {
playerOneNameString = Scripts.GetComponent().Username;
playerOneName.text = Scripts.GetComponent().Username;
}
[RPC]
void AssignPlayerTwo(NetworkViewID viewID, string myName) {
playerTwoNameString = Scripts.GetComponent().Username;
playerTwoName.text = Scripts.GetComponent().Username;
}
}
↧