Trigger events incorrectly registering

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.

Could you please attached your script? :smile: I have this set up and it seems to work appropriately for me, but I am not using Playmaker just Unity :slight_smile:

using UnityEngine;
using System.Collections;

public class TriggerCounter : MonoBehaviour {

    private int triggerCount = 0;
    private bool countTrigger = true;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    void OnTriggerEnter2D ()
    {
        countTrigger = false;
        Debug.Log ("countTrigger bool is set to: " + countTrigger);
        triggerCount += 1;
        Debug.Log ("The trigger count is now: " + triggerCount);
    }

    void OnTriggerExit2D ()
    {
        countTrigger = true;
        Debug.Log ("countTrigger bool is set to: " + countTrigger);
        triggerCount -= 1;
        Debug.Log ("The trigger count is now: " + triggerCount);
    }
}

Apologies, I shouldn’t have cross-posted. I still don’t know what was/is going wrong, but simplifying one FSM and deleting another seems to have the count working for now, touch wood. Thanks for your reply, though.