In order for my character to walk around with the sandwich I parent the sandwich to my character.
This disables the character and the sandwich collider interaction.
Destroying the sandwich does not trigger OnTriggerExit. Moving the sandwich when it is parented to my character does not trigger OnTriggerExit.
The ONLY way I’ve found is to unparent, wait, move the sandwich to a magical place far away, wait, and THEN destroy my sandwich safely when it is eaten. I tried WaitForEndOfFrame() but that was too fast.
This is such a finicky thing that I’d almost have to say it is useless when you have to account for edge cases like this.
I’m using a Utility AI so tying up loose ends like this manually is really a pain in the ass. Is there a better way?
IEnumerator Destroy() {
//todo this is kinda awful, used to trigger ontriggerexit
go.transform.parent = null;
yield return new WaitForSeconds(0.2f);
go.transform.position = new Vector3(0, 10000, 0);
yield return new WaitForSeconds(0.2f);
GameObject.Destroy(go);
}
A sandwich floating around the world waiting to be interacted with is a different sandwich than the one you’re carrying… there’s no shame making it a completely different thing, with different properties, such as lacking a collider. No shame in making it switch out to a completely different thing.
When you walk up to the sandwich OnTriggerEnter works. You pick it up. Eat it. Destroy it. No OnTriggerExit.
My character might not eat the sandwich. He might get shot and drop the sandwich. Now I have to swap it back to a different sandwich?
The only alternative I can see is making a public method I call in OnTriggerExit and calling that directly when I destroy my sandwich. That means I have to keep a reference to the collider in OnTriggerEnter. Maybe multiple references to different colliders.
I’m having to do all this because I was once again lured into using a feature in Unity that has so many edge cases I might as well implement it completely myself.
I’ve looked into it and people have asked for OnTriggerExit to be called on Destroy forever.
Absent from all the above is you stating what you expect to happen in a complete holistic sense.
Whether OnTriggerExit gets called is in the weeds, down with all the bugs and cigarette butts.
Talk higher up the food chain.
Talk a little about why the approaches used in the first three most popular pick-up-item tutorials on Youtube are unsuitable for your use case, because the AR/VR/XR boys don’t seem to have any problems doing it.
Please don’t respond if you’re not willing to read my post. I answered you so suggesting the same thing again isn’t going to help. I completely explained the situation so if you don’t understand you didn’t read it.
I’m not making a coin pickup like the “first three most popular pick-up-item tutorials on Youtube”. Your response is shitty and saying other people don’t have problems isn’t helpful. My character picks up items and walks around with them in their hands. Puts it down and uses it.
Literally everything I want to happen is completely spelled out.
I’m walking up to a sandwich, OnTriggerEnter sets a variable.
I pick it up, parenting it to the character, and eat it before destroying it.
It sure would be nice if OnTriggerExit fired when I Destroy my sandwich, it sure would be nice if I could do what I want in OnDestroy, but I can’t and I explained why. Now I have to handle it MANUALLY based on the OnTriggerEnter colliders.
There’s the whole thing. If you don’t have a suggestion for how to fire OnTriggerExit you’re not being helpful. I know how to move forward, I just don’t like the implementation I’ll have to use.
You can spend your time reexplaining things to them if you want to, and if you want to turn the other cheek you can take the “these people don’t have any problem” also. I appreciate you derailing the thread with a non sequitur.
Take a chill pill mate. You really haven’t explained why you can’t use OnDestroy(), as you’ve only posted a small snippet of your code.
You also haven’t explained what you’re trying to do in OnTriggerExit, so post the full code for us to look at and make suggestions, rather than get mad with the suggestions we’ve made so far.
When you seek help don’t get mad at those who are spending voluntary time to assist you.