How to show players name in UI panel once game starts?
Options
I'm trying to make a one vs one game, but I am having trouble displaying both players names.
I have made a simple game scene, that launches only when 2 players are joined (min 2 players, max 2 players).
In the very beginning I want to have a UI panel (with text) displaying each of their names, which goes away after few seconds.
What would I need to do to accomplish this?
I've made a reference to the two text fields that I want replaced with the Photon.playerName of each player, but every time I try only one of the text fields gets filled with the right name (other is blank). I am quite new to online development, but The code I've written is as follows
private GameObject IntroductionPanel;
private Text Player1text;
private Text Player2text;
// few other variables
void Awake () {
IntroductionPanel.gameObject.SetActive (false);
playerID = PhotonNetwork.player.ID;
SpawnPlayers();
}
void SpawnPlayers () {
if (playerID == 1) {
player1 = (GameObject)PhotonNetwork.Instantiate (prefab.name, Player1Spawn.transform.position, Player1Spawn.transform.rotation, 0);
}
if (playerID == 2) {
player2 = (GameObject)PhotonNetwork.Instantiate (prefab.name, Player2Spawn.transform.position, Player2Spawn.transform.rotation, 0);
}
SetupIntroductionPanel ();
StartCoroutine (MoveMainCamera ());
}
void SetupIntroductionPanel () {
// set the players name here (Where I'm stuck)
if (playerID == 1) {
Player1text.text = PhotonNetwork.playerName;
}
if (playerID == 2) {
Player2text.text = PhotonNetwork.playerName;
}
IntroductionPanel.gameObject.SetActive (true);
}
IEnumerator MoveMainCamera () {
// move camera
}
The "SetupIntroductionPanel" function is where I try to do this.
Any help will be greatly appreciated! Thank you.
I have made a simple game scene, that launches only when 2 players are joined (min 2 players, max 2 players).
In the very beginning I want to have a UI panel (with text) displaying each of their names, which goes away after few seconds.
What would I need to do to accomplish this?
I've made a reference to the two text fields that I want replaced with the Photon.playerName of each player, but every time I try only one of the text fields gets filled with the right name (other is blank). I am quite new to online development, but The code I've written is as follows
private GameObject IntroductionPanel;
private Text Player1text;
private Text Player2text;
// few other variables
void Awake () {
IntroductionPanel.gameObject.SetActive (false);
playerID = PhotonNetwork.player.ID;
SpawnPlayers();
}
void SpawnPlayers () {
if (playerID == 1) {
player1 = (GameObject)PhotonNetwork.Instantiate (prefab.name, Player1Spawn.transform.position, Player1Spawn.transform.rotation, 0);
}
if (playerID == 2) {
player2 = (GameObject)PhotonNetwork.Instantiate (prefab.name, Player2Spawn.transform.position, Player2Spawn.transform.rotation, 0);
}
SetupIntroductionPanel ();
StartCoroutine (MoveMainCamera ());
}
void SetupIntroductionPanel () {
// set the players name here (Where I'm stuck)
if (playerID == 1) {
Player1text.text = PhotonNetwork.playerName;
}
if (playerID == 2) {
Player2text.text = PhotonNetwork.playerName;
}
IntroductionPanel.gameObject.SetActive (true);
}
IEnumerator MoveMainCamera () {
// move camera
}
The "SetupIntroductionPanel" function is where I try to do this.
Any help will be greatly appreciated! Thank you.
0
Answers
-
Hi
you need to listen to other players joining using "OnPhotonPlayerConnected()" and "OnPhotonPlayerDisconnected()" to catch other players name.
You can find a full working UI sample with Player listing and Room Listing here I would recommend you get it on a fresh project, check it out and take it as a base to implement your UI system.
Let me know if you have more questions.
Bye,
Jean0 -
Thank you for your response, but i'm having trouble because the function "OnPhotonPlayerConnected()" is handled in a different scene (the searching or finding a match scene)(scene number 4), and when 2 players are connected in the same room in that scene, the scene changes and goes to the "Game" scene(scene number 5). When I add the "OnPhotonPlayerConnected()" in the Game scene nothing happens / doesn't get called because no new players connect.
In the "Searching Game" scene where I have the "OnPhotonPlayerConnected()" function, I don't know how to access the UI Panel (which is in the "Game" scene) where I want to show each of the players names.0 -
You can use [DontDestroyOnLoad] API on Game Object which have [OnPhotonPlayerConnected()] function in [Searching Game Scene].
Then Access [Game Scene] UI pannel to that survive object.
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html0 -
@Mir
Thanks for the response I've tried that and it works just fine, but now the actual part I'm having trouble on is actually "accessing" the players names. I don't know how to get each players name and set them to the right text spot on the panel (player1text.text = player1Name, player2text.text = player2Name).
What should I do? Make a for loop or a foreach loop on the OnPhotonPlayerConnected function? And if so how do I get each players names and set them accordingly so their names pop up in the right spot?
0 -
I think PhotonNetwork.playerList is what you require. It lists all the players in the current room, including the local player.
0