- Home /
Destroy/Kill AI, Without Errors?
Hi :)
When my AI reach 0 health, I use Destroy() to kill them. Because there a many things going on within it, I get a lot of errors as I am stopping scripts half way through. And it's not just the AI getting destroyed that is giving the errors. Other scripts using the now destroyed object, can no longer find it and start freaking out.
I would love to know how other people get around this. Do I have to try and catch all the possible errors, how is this done(js)? Or is there a better way of killing them, than simply destroying them?
Thanks for your time :) Magnus
Never, I repeat - NEVER, use try catch to just hide unexpected errors. Error is a good thing, because this is the way your program begs you for attention, saying 'pal - you did this wrong! correct me plz...'
Another thing is when you expect an error, and know why it can happen, and know there is no way to prevent this in your specific situation. Then try and catch, because this is the only way.
Unfortunately, it's hard to give you one general advice, without knowing your code inside out... You just have to think on alternative solution. For example, if you reference your object in another script, a good idea after destroying it, might be to set aiIsDead variable on an empty game object to true. Or maybe you should test for null before actually accessing any member of your already destroyed script?
Your answer
![](https://koobas.hobune.stream/wayback/20220613122949im_/https://answers.unity.com/themes/thub/images/avi.jpg)