- Home /
Get the Player that Clicked an Object in Multiplayer?
So, I'm using IPointerClickHandler on an object
public void OnPointerClick(PointerEventData eventData) // 3
{
Debug.Log("Click!");
}
This is a multiplayer game, so each play has a Player character. What would be the best way to get the player character that clicks an object? I'm hoping not to have global variables to do this.
Essentially each player has a tooltip that I want to populate with information on things they click.
Answer by LK84 · Jan 06, 2017 at 10:38 AM
If every Player has localAuthority you can use NetworkInstanceId https://docs.unity3d.com/ScriptReference/Networking.NetworkInstanceId.html. (I assume the script with the method OnPointerClick is a NetworkBehaviour
public void OnPointerClick(PointerEventData eventData) // 3
{
CmdSendClickToServer(netId)
}
[Command]
void CmdSendClickToServer(NetworkInstanceId Id)
{
//I assume you have created a class called Player:
Player clickingPlayer= NetworkServer.FindLocalObject(Id).GetComponent<Player>();
}
If you want to know the clicking Client directly you can also look into https://docs.unity3d.com/ScriptReference/Networking.NetworkConnection-connectionId.html. But if each Player has localAuthority, that would be redundant.
Answer by pfnienow69 · May 27, 2020 at 12:18 PM
(Unity) I have a similar problem, but it is in a local structure, using the new Input System and the MultiplayerEventSystem. I have 4 players and they all have the same buttons for selection (in this case the same Root). How do I identify the player who pressed the button (OnClick)? I don't know what kind of return parameter to ask to be able to link to the player who made the click action. If possible, could you briefly demonstrate a script? Thank you!
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
How to write a multiplayer game that uses a dedicated server? 0 Answers
Networking pattern for team-based play 1 Answer
Networking Camera not attaching to instantiated object 2 Answers
Animation over network using rpcs 0 Answers