Why is OnTriggerEnter2D executing multiple times before I get an OnTriggerExit2D?

So basically I’m working on a 2D game and trying to make it so that when my character is standing in fire he loses 1 HP every 1 second, and when he gets out he stops losing HP. I got everything working by basically connecting this script to the fire…

void OnTriggerEnter2D (Collider2D other)
{
	if (other.gameObject.name == "character")
	{ InvokeRepeating("HealthChange", 1, 1); }
}

Which calls this function…

void HealthChange ()
{ ScreenInterface.playerhealth += healthamount; }

And is killed by this…

void OnTriggerExit2D (Collider2D other)
{
	if (other.gameObject.name == "character") 
	{ CancelInvoke ("HealthChange"); }
}

My fire, for now, is a single sprite with a simple Box Collider 2D attached to it, and my character is a single sprite with a Polygon Collider 2D and a Rigidbody 2D. No animations or anything yet.

I have been working under the (possibly mistaken?) notion that every OnTriggerEnter2D requires an OnTriggerExit2D before there can be another OnTriggerEnter2D. In other words, one collider can come “in” to another collider, but it can’t come “in” again until it goes “out” first. But upon testing I realized that I’m actually getting about 5-10 (it seems to vary) OnTriggerEnter2D executions right away when I come in to the collider before I get a single OnTriggerExit2D when moving out. This is, of course, making my character lose 5-10 times as much health since InvokeRepeating gets executed 5-10 times.

Obviously my question is… what am I doing wrong? Although another question related to that might be… am I on the right track, or should I be doing something completely different to make this fire thing work? I looked at OnTriggerStay2D but I wasn’t sure how to make that work with the 1 second period, not to mention that I suspect that if OnTriggerEnter2D is getting executed many times, OnTriggerStay2D might end up messy like that as well. I guess there might be some issue with my colliders themselves?

PS. I literally just started game development with Unity 4.3 and my only real programming background is web programming so a lot of this is very, very new stuff to me…

Yes OnTriggerEnter/Exit() fire once. Also better using [tags][1] instead of names. Can't see the problem though [1]: http://docs.unity3d.com/Documentation/Components/Tags.html

–

Hmm) Could you upload your project? I'm curious what's happening

–

How would I go about doing that? I'm a bit of a n00b here, not really sure how to upload an entire project.

–

put your assets folder into archive and upload it somewhere) mediafire for example Or you can make unitypackage Assets-> ExportPackage

–

Ok, had to cut some stuff out because I had downloaded the Unity 4.3 demo project and that ended up in my assets folder... let me know if this works... [Unity package here][1] [1]: http://www.negativeworld.org/andrew-nee/andrew-nee-game.unitypackage

–

1 Answer

1

Problem in InvokeRepeating. Apparantly, you can’t set 0 as start time. change it to 0.1f for example and it will work properly.

P.S. Your code is a MESS. Seriously, do not show it to anybody… ever.

Oooh that totally worked, thanks! I have no idea WHY it worked, but it worked. And yeah, I know my code is messy, I've mostly just been trying to figure out what the F I'm doing and haven't been too concerned about keeping it clean. But I plan to clean everything up before I start any serious development. In fact I'll probably just start from scratch.

–

No problem, by the way http://forum.unity3d.com/threads/24154-Inheriting-functions-like-Start-or-Update

–