Health Bar Not in Sync



  • Hi,
    First of all, sorry English is not my native language thus I will try my best explaining what I did and what the errors were.
    I am trying to understand how the health sync. Thus I created new unity file and I added using TanksMP; before I declare my public variable such as int maxHealth, etc. I have set my maxHealth to be 1000 in the declaration as well as in the inspector, in the Awake function, I added GetView().SetHealth(maxHealth);, in the Start function, I added OnHealthChange(GetView().GetHealth());, then I added

    public override void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
        {
            
            PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer;
            if (player != photonView.owner)
                return;
    
            
            OnHealthChange(player.GetHealth());
        }
    

    finally in the TakeDamage function I added

    int health = GetView().GetHealth(); 
    health -= (int)amount;
    
            if(health <= 0)
            {
                GetView().SetHealth(maxHealth);
    
                //PlayerDie();
                this.photonView.RPC("PlayerDie", PhotonTargets.AllViaServer);
    }
    else
            {
                GetView().SetHealth(health);
                
            }
    

    However, when I pressed play in the unity editor, the health bar is not even full. Is there any way to fix this?
    Thank you



  • Hello @dennist,

    I could not to find an issue with your code, as that's 1-1 copy pasted from the Player class. Please also check if your SetHealth and GetHealth Player extension methods responsible for writing and reading to the player custom properties are present.

    Lastly, please put a Debug.Log in the OnPhotonPlayerPropertiesChanged method to find out whether the method is actually called, and what the current health value is.



  • Hi @Baroni,

    Sorry, I was out of town last week. Just got back now, I will try your solution and report back here if the error persists.

    Thank you


Log in to reply