Syncvar Hook on dedicated Server

Hi guys,
I’m having a hard time wrapping my head around Unet system on a dedicated server.Most of the threads or videos are using Unet out of the box.

I just want to use a simple syncvar for my health player, hook this up to an “update life” function.
What is happening in my client instance is that the lifebar is never updated -_-

Now, can you tell me what I am doing wrong ?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class Health : NetworkBehaviour
{
    public const int maxHealth = 100;
    [SyncVar(hook ="UpdateLife")]
    public int currentHealth = maxHealth;
    public Image healthBar;

    //apply Damages
    public void TakeDamage(int amount)
    {
        if (!isServer) {
            return;
        }
        // apply damages on server instance
        currentHealth -= amount;
        // check for dead player
        if (currentHealth <= 0) {
            currentHealth = 0;
            RpcRespawn ();
        }
    }

    //update sync health
    void UpdateLife(int health){
        currentHealth = health;
        if (isServer) {
            RpcOnChangeLifeBar (currentHealth);
        }
    }
      
    [ClientRpc]
    void RpcOnChangeLifeBar( int health){
        if (isLocalPlayer) {
            healthBar.fillAmount = health / maxHealth;
        }
    }
      
    [ClientRpc]
    void RpcRespawn()
    {
        if (isLocalPlayer)
        {
            // respawn
            transform.position = Vector3.zero;
            // reset life
            currentHealth = maxHealth;
        }
    }
}

UpdateLife is called on the client, which itself can not call the RPC, only the server can call an RPC. Move the logic in the RpcOnChangeLifeBar into the UpdateLife method, and remove the RPC. Good Luck!

Thank you for your reply @Jos-Yule :slight_smile:
But now the fill amount directly go down right to 0 :hushed:

Though I can see my health decreasing by the right incrementation…
[EDIT]
Never mind, I just realized that I was using Int for my health, and that my fill amount should be a float from 0 to 1… -_-

But another weird thing is that even if my player is respawning at the right place, his health doesn’t re-initialize. :confused: