- Home /
Photon How to get playername from a gameobject
How could i get the PhotonNetwork.playerName from a gameobject in photon?
Answer by Firedan1176 · Jul 15, 2014 at 07:21 PM
The simple answer:
GetComponent<PhotonView>().owner.name;
You'll need to grab a GameObject first.
My full explanation:
If it's something like a player that joined the game, you first need to make sure they set their name on their client. For example, you can create a TextField to enter in a username. When they enter their name, you can then set PhotonNetwork.player.name
equal to the input. Something like:
PhotonNetwork.player.name = GUILayout.TextField(new Rect(0, 0, 100, 100), PhotonNetwork.player.name);
I may have an error above; just an example.
When you set this, any game you join will be synchronized with that name. You can grab someone's name from another client by doing something like this:
As long as all players in the game have the tag 'player'...
GameObject[] playersInGame = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject player in playersInGame) {
Debug.Log("There is someone named: " + player.GetComponent<PhotonView>().owner.name + " in the game!");
}
Now keep in mind you'll also probably want to do some checking to make sure the player has a PhotonView (Which would be bad if they didn't D:).
But other than that, you should be good to go.
If you have any problems, comment and I'll try to help.
Answer by mes007 · Mar 07 at 03:29 PM
@Firedan1176 If for example I have a client named: test_01 Also, I have object with view ID. When I grab the object, I want to know the player who just grabbed it.
So this is what I added to my XRGrabNetworkInteractable.cs:
protected override void OnSelectEntered(XRBaseInteractor interactor)
{
//Give the ownership to the client who is grabbing the network object
photonView.RequestOwnership();
Debug.Log($"Object name: {photonView.gameObject.name}");
Debug.Log($"User ID: {photonView.Owner.UserId}");
base.OnSelectEntered(interactor);
}
This is raising an Error: Object reference not set to an instance of an object XRGrabNetworkInteractable.OnSelectEntered (UnityEngine.XR.Interaction.Toolkit.XRBaseInteractor interactor) (at Assets/Scripts/Network/XRGrabNetworkInteractable.cs:28) UnityEngine.XR.Interaction.Toolkit.XRBaseInteractable.OnSelectEntered (UnityEngine.XR.Interaction.Toolkit.SelectEnterEventArgs args) (at Library/PackageCache/com.unity.xr.interaction.toolkit@2.0.0-pre.7/Runtime/Interaction/Interactables/XRBaseInteractable.cs:775)
What do you suggesting ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613153508im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
(C#) Help with Multiplayer Features - Photon Unity Networking 0 Answers
Photon networking; how to check what position/number the player spawned 1 Answer
Problems running client with Photon code on other computers 0 Answers
Name display problem 1 Answer
PHOTON/ Syncing player name and sprite to other players? 0 Answers