• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 25, 2018 at 09:40 AM by hexagonius.
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unassigned Reference Exception ? 0 Answers

Missing Reference Exception @ GuiSkin...Gui Button 1 Answer

MissingReferenceException: The object of type 'CircleCollider2D' has been destroyed but you are still trying to access it. 1 Answer

Particle Emitter error, Missing reference exception. 1 Answer

Inspector Error 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges