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;
}
}
}