- Home /
Question by
lmuniz · Jul 28, 2017 at 04:14 AM ·
c#networkingnullreferencehealth
Why the Network Health script is always null
I have this code for when a collision happens calls a health script, but the is always == null
void OnCollisionEnter(Collision collision)
{
Health health = collision.gameObject.GetComponent<Health>();
Debug.Log("HERE WORKS");
if (health != null)
{
Debug.Log("NEVER GET HERE");
health.TakeDamage(10);
}
Destroy(gameObject);
}
My health script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Health : NetworkBehaviour
{
public const int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth= maxHealth;
public RectTransform healthBar;
public void TakeDamage(int amount)
{
if (!isServer)
{
return;
}
currentHealth -= amount;
Debug.Log("Entrou: " + currentHealth);
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead: " + currentHealth);
}
healthBar.sizeDelta = new Vector2(currentHealth, healthBar.sizeDelta.y);
}
void OnChangeHealth(int health)
{
healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
}
}
I'm following this guide and i think did every steps. https://unity3d.com/pt/learn/tutorials/topics/multiplayer-networking/networking-player-health?playlist=29690
Comment
Your answer
Follow this Question
Related Questions
Getting a mix of warnings in multiplayer 0 Answers
Unet Not Syncing Health for Host 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Networking: Client-spawned Objects do not appear on Server 0 Answers