Best way to find which player is 'my' player in scene?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on Fusion.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Best way to find which player is 'my' player in scene?
Wobbles67
2022-06-22 10:08:38
I need a way to find which player in the scene is 'my' player when clicking a button on a user interface. I can't use 'hasInputAuthority' directly because the script is running on the UI and not the player object.
The only way I can think of is to search through every player object in the scene and check which one 'hasInputAuthority', but this seems a very inefficient way of doing it, especially if there are a lot of players in the scene. Is there a better way?
Edit: To clarify a bit, I want to perform an action on 'my' player when clicking the UI button. For example, changing clothes. I'm thinking RPC is the way to go with this but I'm still not sure how to call it since I don't have an instance of 'my' player object.
Update: I have now also tried:
NetworkObject player = runner.GetPlayerObject(runner.LocalPlayer);
But this just returns null.
Comments
Okay, I have a solution that works, don't know if it's the best. Whilst the above doesn't work, the following does seem to find your own player:
GameObject myPlayer = GameObject.Find(runner.LocalPlayer.ToString());
Isaac_Augusto
2022-06-23 10:00:51
Hi,
NetworkObject player = runner.GetPlayerObject(runner.LocalPlayer);
The above request you to first set the player object. An example would be setting when you spawn the player character. You can set only one NetworkObject
for each player.
NetworkObject playerObj = runner.spawn(playerPrefab, playerRef);
runner.SetPlayerObject(playerObj, playerRef);
Note that this can only be done on the Host/Server.
In Shared mode, each player can set it own PlayerObject
as long as they have authority over that NetworkObject
.
Using PlayerObject
should be more safe and optimize than GameObject.Find
Isaac Augusto
Photon Fusion Team
Thank you. This works perfectly.
Back to top