- Home /
OnCollisionExt not working/ Alternate collision detection
Okay, here's my problem. I have two gameobjects with the tag "selectable". I need to check if they are colliding. Here's part of my script:
void OnCollisionEnter(Collision other){
if(other.gameobject.tag.Equals("Selectable")){
CollidingWithSelectable = true;
}
}
void OnCollisionExit(Collision other){
if(other.gameobject.tag.Equals("Selectable")){
CollidingWithSelectable = false;
}
}
The thing is, OnCollisionExit isn't called when one of the objects is destroyed. Is there any way to check collisions WITHOUT OnCollisionExit or OnCollisionEnter?
I have tried CastSphere, but (as far as I know), there is no way to check what object it is colliding with.
Please help!
Spherecast() returns the object hit in the RaycastHit passed, but I don't think Spherecast() is what you are looking for. If a sphere is a good enough check, try Physics.OverlapSphere().
Answer by The Kracken · Dec 10, 2013 at 02:05 AM
Your problem might be the code.
void OnCollisionExit(Collision other){
if(other.gameobject.tag.Equals("Selectable")){
CollidingWithSelectable = true;
}
}
CollidingWithSelectable
is never set false. Add a Debug.Log("Exiting collision");
to see if OnCollisionExit
is being called.
Well, actually, I didn't exactly copy the code, so this "typo" isn't in the code. Sorry about that.
The thing is, OnCollisionExit IS called normally and it works fine, but when the gameobject it's colliding with is destoyed, it doesn't seem to be called.
ah ok, then what i would do is put a function call in OnDestroy() in the object that gets destroyed to do whatever you need.