This is a problem I raised on the Playmaker forum, but I am raising it here because it isn’t a Playmaker issue. This is the original thread: Global trigger events
I didn’t have a handle on what was happening when I started that thread, so I’ll be clearer now. A gameobject is checking trigger exits and trigger enters in order to count the current number of triggers it’s interacting with. It is collecting exits properly, but it is counting enters every time it moves within a collider - thus creating vastly more +1s than -1s and getting an absurd number for the trigger count.
This is certainly not a Playmaker issue because I have replicated it in a completely fresh, bare bones project.
Starting from a fresh 2d project, add three sprites. Give each rigidbodies2d and circlecolliders2d. Check “is trigger” in two of the sprites and make their colliders a few times larger than the sprites themselves - enough to have them overlap and cover a fair portion of screen. (See attached screenshot.) Leave the third sprite’s “is trigger” unchecked and leave the radius of its collider unchanged. This third sprite is the one that will check for triggers.
Now add the following script to that third sprite:
using UnityEngine;
using System.Collections;
public class TriggerCounter : MonoBehaviour {
public bool onTrigger;
public int triggerCount = 0;
void OnTriggerEnter2D()
{onTrigger = true; triggerCount++;}
void OnTriggerExit2D()
{onTrigger = false; triggerCount–; }
}
(I’m rubbish at code and this basically copy-pasted.)
If the third sprite is within both of the other two’s colliders, triggerCount correctly shows as 2. But as soon as you drag the sprite around in the screen, you can see it incrementing triggerCount continually. You can see the exits are working fine by, for example, changing the onTrigger=false to increment by a million - you can count the number of exits by counting the number of millions.
So what is happening? The same effect happened using a similar increment/decrement method in Playmaker. It looks like a massive bug, but no one else is reporting it, so I’m assuming either I’ve made some elementary blunder or something’s got corrupted. I hope this description is clear.
