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!