Unity 3d photon pun UI

56 Views Asked by At

I have a problem with my game and honestly doesn’t know how to fix it and don’t know what to do now.. I would really appreciate if you can at least give me any idea of your solution.

I have 3d game in Unity. I’ve connected photon pun 2. I have a prefab with my player. Player have an a big UI system, with health, stamina, hotbar, also some images, bars etc. The problem is, when I’m connecting to player to game, UI starts showing for everyone.

For example, we have player A & player B. Player A, Player B, have their own health, stamina, hotbar. When Player A is in game, everything looks fine, but when Player B connects to the game, Player A start see Player B UI, and player B start to see Player A UI, and these UIs are like overflowing each other.

I’ve tried many solutions. Chat GPT gave me a script where when player is connecting, scripts finds similar UI elements and just deleting it, but here is a problem with this solution, it’s deleting it for every player, so thats mean, that player A, doesnt have his own UI at all when player B connects.

Is there any solution, like, just to hide UI other players from each other?..

I’m not sure I’ve explained everything good, but i hope at least someone could give me at least an idea…

Also I would like to pay attention, that UI elements already turned ON in player prefab. That means that I'm not innitialazing them throw photon. I heard that I can initialize them throw photon like a child of player, but I'm not sure will it work because I have a bunch of scripts which are need to "speak" with UI elements (sorry forgot this word in en :< )

1

There are 1 best solutions below

0
Wadish Developer On BEST ANSWER

I FINALY FIXED IT AFTER A MONTH OF TRYIG. I HAVE A HAPPY TEARS RN!!!

using Photon.Pun;
using UnityEngine;

public class PlayerUIManager : MonoBehaviourPun
{
    public GameObject playerUI;

    private void Start()
    {
        UpdateUIVisibility();
    }

    private void UpdateUIVisibility()
    {
        if (photonView.IsMine)
        {
            if (playerUI != null)
                playerUI.SetActive(true);
        }
        else
        {
            if (playerUI != null)
                playerUI.SetActive(false);
        }
    }

    public override void OnPlayerEnteredRoom(Photon.Realtime.Player newPlayer)
    {
        UpdateUIVisibility();
    }

    public override void OnPlayerLeftRoom(Photon.Realtime.Player otherPlayer)
    {
        UpdateUIVisibility();
    }
}