- Home /
OnCollisionExit() firing when object has not exited
I have two sprites, a Player and an NPC. When the player enters the NPC's BoxCollider, a story starts. When the player exits the NPC's BoxCollider, I want the text to fade out.
The issue I'm having is that after selecting a choice in the story, the text fades (effectively doing what's in OnCollisionExit2D). The Player has not exited the NPC's BoxCollider, but the OnCollisionExit2D is firing. What am I doing wrong?
Here's a snippet from the NPC script:
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Player" && GameMaster.GM.politeness==0)
{
StartStory();
isColliding = true;
}
}
void OnCollisionExit2D(Collision2D other)
{
if(other.gameObject.tag == "Player")
{
FadeText(2);
//GetComponentInChildren<Canvas>().enabled = false;
isColliding = false;
}
}
void FadeText(int fadeTime)
{
foreach (Text txt in GetComponentsInChildren<Text>())
{
txt.CrossFadeAlpha(0, fadeTime, false);
}
}
And here's a link to a video of it happening in action: OnCollisionExitMystery
Have you checked if the Player
's BoxCollider
perhaps disappears for some reason, making the text fade away?
Also, make isColliding
public and study it's behavior whilst you play out the scene and perhaps you'll notice something interesting.
I checked out the video and you should try commenting this line out: FadeText(2);
. If the text still fades out you know it's not this script's fault. However if it doesn't fade then you know for certain that there's something making OnCollisionExit2D
run.