0
votes

I'm making a multiplayer FPS game with Photon PUN 2 and have been following this tutorial series by Rugbug Redfern. I'm at the stage where I'm starting to implement gun mechanics and UI (ammo, health, etc) and I have multiplayer working, although I don't entirely understand the inner workings of PUN 2.

My issue is that I'm trying to get an ammo counter shown up on the players screen, to show how much ammo they have left, and when more then one player joins the game, there seems to be multiple ammo counters (see bottom left of image below), one of the original player, and one of the newly joined player [although it doesn't update because it's not synced across the network]. This is obviously not good and I don't really know what to do. I have a player prefab that gets instantiated when a new player joins the game, there is a UI Canvas and the ammo counter in that prefab as well. So when two instances of the player are in the same scene, there are 2 canvas's, and thus 2 ammo texts. How would I go about making sure there is only one, the one of the own player? Would a singleton implementation work? I'm new to to this and would love any help, thanks!

ammo counter being whacky

1

1 Answers

1
votes

A. If the UI ammo counter Canvas is part of the player prefab, simply deactivate its root GameObject, remove it or disable the Canvas or hide it for remote players. Or the opposite, only show it for the local player: by default it's deactivated/disabled/hidden then active/enable/unhide it.

B. If you separate the UI ammo counter into its own prefab or even make it part of the scene and just enable/unhide it when joined.

Simply do not instantiate the UI ammo counter prefab for remote players. You are probably doing it in the OnPlayerEnteredRoom callback, or doing it for all previously joined players in OnJoinedRoom callback, don't.

Just do it once, for the local player, inside OnJoinedRoom callback.