- Home /
Destroy(this) not working properly
I don't think that my Destroy(this) function is working properly, here's the script
using UnityEngine;
using System.Collections;
public class NpcHandler : MonoBehaviour {
public string npcName;
public int npcLevel;
public int npcExpWorth;
public int npcCurrHealth, npcMaxHealth;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(npcCurrHealth < 1) {
Destroy(this);
}
}
}
Now, once he runs out of health the other scripts can no longer reference from it, although the object stays there and the scripts are still attempting to reference, not sure what's going on here.
Yes, the script is attatched directly to the game object.
Answer by clunk47 · Jun 22, 2013 at 12:53 AM
Are you trying to destroy the script, or the gameobject?
(this) is the script. (this.gameObject) is the gameObject. or just Destroy(gameObject). Sorry if I didn't understand your question.
Answer by Sisso · Jun 22, 2013 at 12:53 AM
You are destroying the Component not the Object. Use Destroy(gameObject).
When destroy things it is better to explicitly call the desired method Component.Destroy(this) or GameObject.Destroy(gameObject)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Error or Bug in unity 0 Answers
When I kill one enemy, the rest disapear, but still shoot 1 Answer
Solve me this unexpected token error? 0 Answers
turret not getting destroyed 1 Answer