Player names with photon PUN
i have been working for hours to try and figure out how to display player names above a player, but no matter what i try all the players names change to your name. they wont sync across network.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerNameController : Photon.MonoBehaviour {
[HideInInspector]
public int MyIndex;
string PlayerName;
[HideInInspector]
public CameraController CameraControl;
InputField PlayerNameInput;
void Start()
{
MyIndex = Random.Range(-99999, 99999);
CameraControl = GameObject.Find("Main Camera").GetComponent<CameraController>();
PlayerNameInput = GameObject.Find("PlayerNameInput").GetComponent<InputField>();
}
private void Update()
{
// Doing the right thing
PlayerName = PlayerNameInput.text;
GameObject[] Names = GameObject.FindGameObjectsWithTag("PlayerTexts");
if (PlayerNameInput != null && photonView.isMine == true)
{
transform.GetComponent<TextMesh>().text = PlayerName;
}
// Sending the variables
photonView.RPC("NameReceive", PhotonTargets.All, PlayerName, MyIndex);
PlayerName = photonView.owner.name;
}
[PunRPC]
// Recieving the data
void NameReceive(string PlayersName, int MyInt)
{
// Checking if Index is same
GameObject[] i = GameObject.FindGameObjectsWithTag("PlayerTexts");
foreach (GameObject thing in i)
{
if (thing.GetComponent<PlayerNameController>().MyIndex == MyInt && photonView.isMine == false)
{
// Changing the text
Debug.Log(PlayerName);
thing.GetComponent<TextMesh>().text = PlayersName;
}
}
}
}
Answer by ChristianSimon · Jan 23, 2018 at 01:00 PM
Hi @ratgamer,
you don't have to use a RPC to synchronize nick names. You can use PhotonNetwork.player.NickName instead, which is automatically synchronized across all clients in the room. To display the correct name on every client, you can use the already attached PhotonView component and get the nick name. To do so you have to use photonView.owner.NickName
in your source code.
How do i change it from rpc to PhotonNetwork.player.NickName ???
Your answer
Follow this Question
Related Questions
Photon - Cannot join same room 1 Answer
Photon Player Mover Failure 1 Answer
Both players reach same time how to get result draw in photon unity 1 Answer
Photon Player And Weapon View Error 0 Answers
What is wrong here? 0 Answers