OnTriggerEnter and OnTriggerExit are activating twice

I’m building a super barebones version of a game to build from, so i have a rectangle (the player) that bumps into another rectangle (interaction test). The idea is that when the player is colliding with the rectangle, an object is instantiated. When the player exits collision, the object is destroyed.

Now I used both OnCollisionEnter/Exit and OnTrigger etc. When using OnCollision, the object would appear and disappear as it was supposed to MOST of the time, but 1/10 exits wouldn’t destroy the object.

Using Trigger, they are both (enter/exit) activated twice. I added a print function that just outputs on enter/exit, and it prints twice. That means that two objects are instantiated, and while two destroy commands are sent, only the second object is destroyed, leaving one behind.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollisionUseTest : MonoBehaviour {

    public GameObject testSpawn;
    GameObject clone;

    public void OnTriggerEnter(Collider collider)
    {
        clone = (GameObject)Instantiate(testSpawn);
        print("something else");
    }

    public void OnTriggerExit(Collider collider)
    {
        Destroy(clone);
        print("something");
    }

    // Use this for initialization
    void Start () {

	}
	
	// Update is called once per frame
	void Update () {
       
	}
}

I’ve messed around with collider size/shape, but it seems to act the same.

Plz help!
Thanks!

This is normal. It is executing for the collider and the collidee.

At the point where your collision code executes, do a check for the col.gameObject.tag or layer and only execute for specific tag names, for example. That way you will only get one instance.

void OnTriggerEnter(Collider col)
{
    if (col.gameObject.tag != "rectangle1") return;

    // spawn object
}

I removed/disabled colliders from Player and then the trigger event was only firing one time.
You can configure layer collision matrix so these colliders don’t interact and see if that works.