- Home /
Text name above player's head in Photon
I've been ripping hair out trying to figure this out.
Basically, I have a basic Unity game setup with Photon for networking.
Each player spawns with a PlayerPrefab, but I'm trying to put their name above their player prefab, for example. Player 1, Player 2, etc text above an object, or player in this case.
I've looked around everywhere but cant quite figure it out, so I'm asking you folks on what would be the fastest, and easiest way to do it?
I don't have any base code snippets for these, since I'm unsure where to even start.
I really need some help, thanks for reading!
Answer by lancer · Sep 11, 2013 at 12:32 AM
I did this same thing a month ago, what you do is make a 3D Text object and put it as a child of the player prefab. Attach this script to the 3D Text object:
using UnityEngine;
using System.Collections;
public class Nametag : MonoBehaviour {
void Start () {
GetComponent<TextMesh>().text = PhotonNetwork.playerName;
}
}
And it should work.
Hmm, I've tried what you said, by using a 3D text, and it seems to work better, but its still not updating the text to show the players username.
I actually think I know why, could it be that I don't have any sort of username login system set up under my Game$$anonymous$$anager?
using UnityEngine;
using System.Collections;
public class Game$$anonymous$$anager : Photon.$$anonymous$$onoBehaviour {
public Transform PlayerPrefab;
void Start ()
{
PhotonNetwork.ConnectUsingSettings("alpha 0.3");
}
void OnGUI ()
{
GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString());
}
void OnJoinedLobby()
{
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed()
{
PhotonNetwork.CreateRoom(null);
}
void OnJoinedRoom()
{
PhotonNetwork.Instantiate("PlayerPrefab", transform.position, Quaternion.identity, 0 );
}
}
Any Ideas how I could setup a simple GUI to enter a Player name in before joining?
Yes, this should get you started:
using UnityEngine;
using System.Collections;
public class NamePicker : Photon.$$anonymous$$onoBehaviour {
string PlayerName;
void OnGUI(){
PlayerName = GUI.TextField(new Rect(Screen.width / 2 - 50, Screen.height / 2, 100, 20), PlayerName);
}
void Update () {
PhotonNetwork.player.name = PlayerName;
}
}
Hmm, when I use that, anywhere, my console gets spammed a million times with this error.
NullReferenceException: Object reference not set to an instance of an object
Not quite sure why, I've tried it on my PlayerPrefab, my Game$$anonymous$$anager, and on its own script and object.
Scratch that, I added public, to the string, and I fixed that error.
I don't know, there is something weird going on here. It worked fine for me, I don't think I can help anymore.
Have you tried posting this in the Photon forum thread? Or E-mailing Photon?
Answer by Sisso · Sep 10, 2013 at 08:23 PM
Dig a little and you will really found many solutions. One of them: http://www.tasharen.com/forum/index.php?topic=130.0
I have done a lot of searching, and that's not exactly what I need sadly.
All I'm trying to do is when a player instantiates their playerPrefab, by joining the game, I want to assign a GUI text, or some kind of text, their PhotonNetwork name, such as Guest1, or Player1. And I'm not to sure where to start, a basic code snippet or tutorial would be great.
I seriously doubt this is a correct way to do it but.
using UnityEngine;
using System.Collections;
public class PlayerName : Photon.$$anonymous$$onoBehaviour {
Vector3 screenPos;
[RPC]
void OnGUI ()
{
if(photonView.is$$anonymous$$ine)
{
return;
}
screenPos = Camera.main.WorldToScreenPoint(transform.position);
screenPos.y = Screen.height - screenPos.y;
GUI.Label(new Rect(screenPos.x-40,screenPos.y-55,100,20),"Name: "+PhotonNetwork.playerName);
}
}
Basically right now, it just displays Name: on top of your player.
How would I go about getting the correct Photon ID/name?
I think it'll just show your name no matter what over every player. What you need to do is tell every other client that is on the server what that string is. You'll need to find a way to grab your player on other clients and set the string over the network to your name.
I know I'm really grave digging here, but since this turned up in my search results pretty quickly, I figure I'll answer your question here.
I like your method of getting the username to display, but I'd pass the correct photon id by way of RPC. If you have a character stats script, then just have an RPC to set the right stats equal to the right things, and it'll handle that globally. In other words, call an RPC that passes the photon player name to the RPC method. Your RPC method sets your string variable "username" equal to the passed string. Then just display the right username.
And you don't need to make OnGUI an RPC - it'll update automatically on every client consistently.
@Coder156, how do i make the text use the correct name with rpcs? I tried this and couldn't figure it out.
Answer by Berthz · Jun 17, 2020 at 05:37 PM
The initial solution by Lancer is not working anymore due to Photon updates. When using Photon 2 you can do the following:
1: As the first solution mentioned you add a 3D text object as a child to the player prefab
2: Create a new C# script and call it SimplePlayerName
3: Add the following code into the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class SimplePlayerName : MonoBehaviour
{
void Start()
{
GetComponent<TextMesh>().text = PhotonNetwork.LocalPlayer.NickName;
}
}
4: Add the script to your 3D text object