- Home /
I have a problem with the healthbar in a network multiplayer
Hi there. I have this "A short summary of the script"
HEALTHBAR:
public const int maxHealth = 100;
public const int maxMana = 100;
public bool destroyOnDeath;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
public int currentMana = maxMana;
public RectTransform healthBar;
public RectTransform manaBar;
void Start ()
{
healthBar =(RectTransform) Hud.HealthBar;
manaBar = Hud.ManaBar;
}
public void TakeDamage(int amount)
{
if (!isServer)
return;
currentHealth -= amount;
if (currentHealth <= 0)
{
if (destroyOnDeath)
{
Destroy(gameObject);
}
else
{
currentHealth = maxHealth;
// called on the Server, invoked on the Clients
RpcRespawn();
}
}
}
void OnChangeHealth (int currentHealth )
{
healthBar.sizeDelta = new Vector2(currentHealth , healthBar.sizeDelta.y);
}
}
HUD
public Transform LifeBar;
public static RectTransform HealthBar;
public static RectTransform ManaBar;
void Start () {
LifeBar = this.gameObject.transform.FindChild("LifeBar");
HealthBar = LifeBar.transform.Find("HealthBar").GetComponent<RectTransform>();
ManaBar = LifeBar.transform.Find ("ManaBar").GetComponent<RectTransform>();
}
BALA
public int Damage; // Use this for initialization void Start () { if (Damage == 0) { Damage = 10; } }
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other){
var hit = other.gameObject;
var health = hit.GetComponent<Health> ();
if ((other.gameObject.name != "BulletMulti")) {
Destroy (this.gameObject);
}
if (health != null) {
health.TakeDamage (Damage);
}
//recuerda poner el && (other.gameObject.tag != "Player")
}
And it gives me an error.
"NullReferenceException: Object reference not set to an instance of an object Health.OnChangeHealth (Int32 currentHealth) (at Assets/Scripts/Health.cs:57) Health.set_NetworkcurrentHealth (Int32 value) Health.TakeDamage (Int32 amount) (at Assets/Scripts/Health.cs:38) Bala.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Bala.cs:27)"
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
The name `OnConnect' does not exist in the current context 1 Answer
Network Manager HUD 0 Answers
Building and Running 1 Answer
[Multiplayer Lobby] Spawned enemies not seen in client's space.... 1 Answer