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!

I don’t know why it is spawned twice, but the way you call it, you’re lucky not to get into an infinite loop.

First off, you should specify where your object is instantiated. Doing it like this just instantiates it at the target location. If the instantiated object has a rigidbody attached to it, it will trigger instantly, which is why I suspect you have problems. Furthermore, you have problems because you have no idea how to implement the behaviour you want correctly. You want something like this:

using UnityEngine;
using System.Collections.Generic;

public class whatever : MonoBehaviour
{
	public GameObject ToSpawn;

	List<GameObject> ObjectList = new List<GameObject>();

	void Start()
	{
		ToSpawn = gameObject;
	}

	void OnTriggerEnter2D(Collider2D col)
	{
		CreateObject(col);
	}

	void OnTriggerExit2D()
	{
		foreach(GameObject GO in ObjectList)
		Destroy(GO);

		ObjectList.Clear();
	}

	void CreateObject(Collider2D col)
	{
		CircleCollider2D current = col.GetComponent<CircleCollider2D>();

		float colSize = col.transform.localScale.x;

		float RandomX = ((Random.Range(0, 9999) < 5000) ? -1 : 1) * (current.radius * colSize + Random.Range(100, 1000)/10f);
		float RandomY = ((Random.Range(0, 9999) < 5000) ? -1 : 1) * (current.radius * colSize + Random.Range(100, 1000)/10f);

		ObjectList.Add((GameObject)Instantiate(ToSpawn, new Vector3(col.transform.position.x + RandomX, col.transform.position.y + RandomY, 0f), Quaternion.identity));
	}
}

Note that I am dealing with game objects of size 10 which have circle colliders.

Also, here is a GIF representing this: alt text