• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by AwesomeX · Sep 10, 2013 at 07:58 PM · networkingtextphotonname

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AwesomeX · Sep 11, 2013 at 12:59 AM 0
Share

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?

avatar image lancer · Sep 11, 2013 at 01:06 AM 0
Share

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;
 }
 }
avatar image AwesomeX · Sep 11, 2013 at 12:47 PM 0
Share

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.

avatar image AwesomeX · Sep 11, 2013 at 01:46 PM 0
Share

Scratch that, I added public, to the string, and I fixed that error.

avatar image lancer · Sep 13, 2013 at 03:59 PM 1
Share

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?

Show more comments
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AwesomeX · Sep 10, 2013 at 08:48 PM 0
Share

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.

avatar image AwesomeX · Sep 10, 2013 at 11:15 PM 0
Share

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?

avatar image Firedan1176 · Apr 29, 2014 at 12:18 AM 0
Share

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.

avatar image Coder156 · Jun 27, 2016 at 02:21 AM 0
Share

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.

avatar image NOVICEEntertainment · Feb 28, 2017 at 10:21 PM 0
Share

@Coder156, how do i make the text use the correct name with rpcs? I tried this and couldn't figure it out.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiplayer Position Sychronization problem in unity2d 1 Answer

Callback handling all PhotonNetworkingMessages 1 Answer

Photon (PUN) Type Serialization Error on RPC 2 Answers

Problem with UFPS and photon 1 Answer

Photon - Show player names. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges