- Home /
This question was
closed Dec 25, 2018 at 09:40 AM by
hexagonius.
Question by
azharm833 · Dec 25, 2018 at 09:28 AM ·
error messageexceptiongame developmentmissingreferenceexception
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class enemyHealth : MonoBehaviour {
public float enemyMaxHealth;
public GameObject enemyDeathFX;
public Slider enemySlider;
public bool drops;
public GameObject theDrops;
float currentHealth;
// Use this for initialization
void Start () {
currentHealth = enemyMaxHealth;
enemySlider.maxValue = currentHealth;
enemySlider.value = currentHealth;
}
// Update is called once per frame
void Update () {
}
public void addDamage(float damage){
enemySlider.gameObject.SetActive (true);
currentHealth -= damage;
enemySlider.value = currentHealth;
if (currentHealth <= 0) makeDead ();
}
void makeDead(){
Destroy (gameObject);
Instantiate (enemyDeathFX, transform.position, transform.rotation);
if (drops)
Instantiate (theDrops, transform.position, transform.rotation);
}
}
Comment
Follow this Question
Related Questions
Unassigned Reference Exception ? 0 Answers
Inspector Error 1 Answer
UnassignedReferenceException 0 Answers
Do I NEED to handle the MissingReferenceException? 1 Answer
Powerup only spawning twice (MissingReferenceException Error) 2 Answers