- Home /
Check For MissingReferenceException
I have an AI that is trying to destroy a target. I have a function called Evade(). If target is null, then perform Evade(). But the problem is when the AI destroys it's target, the target variable doesn't become null, it becomes a MissingReferenceException. The Evade() function is never called, and it doesn't look for a new target. So I need, not only to check if target is null, but if it's missing. How do I do that? I tried this :
if(!target || target == MissingReferenceException){
Evade();
FindTarget();
}
I get an error saying that it's not a value, type, variable, or method group. What should I use instead of MissingReferenceException?
Answer by JPB18 · Dec 30, 2011 at 09:05 PM
Well, you could do something like:
var targethealth : int;
var targetdestroyed : boolean;
if (targethealth <= 0)
{
targetdestroyed = true;
}
if (targetdestroyed = true)
{
Evade();
FindTarget();
}
Hope I've helped =)
Oh yeah. Tried that like 30 seconds before you posted, but thanks. I used GetComponent to find it's health.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062521im_/https://answers.unity.com/themes/thub/images/avi.jpg)