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;
}
}
}
}