Destroy gameobject in collision

Hello, I have to objects in collision. Why if i destroy one of gameobjects the second gameobject is still in collision? Thank you

Try without OnTriggerExit. Make a new bool isOnTrigger assigned before OnTriggerStay() and set it false in Update(). Then in update before setting isOnTrigger false check is it false, if it is execute code inside your current OnTriggerExit.

Looking back it looks like this is expected behavior so id deleting the GameObject that causes the trigger you have to use some other method. Either move the game object “enemy” far away just before destroying or I just check this method:

if(DestroyingEnemy != 0f && Time.time >=  DestroyingEnemy)
		{
			BeenTriggered = false;
			DestroyingEnemy = 0f;
		}

DestroyingEnemy is a public float, when I call Destroy for my Enemy (I set to 1 second) I then have a reference to the script that contains the lines above and send it Time.time + 1.

using UnityEngine;
using System.Collections;

public class EnemyTrigger : MonoBehaviour {

	public Enemy theEnemyScript;

	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Enemy")
		{
			Debug.Log ("hit the guard tower");
			GameObject myEnemy = GameObject.FindGameObjectWithTag(other.tag);
			Destroy(myEnemy, 1f);
			theEnemyScript.DestroyingEnemy = Time.time + 1f;
		}
	}

	void OnTriggerExit()
	{
		Debug.Log ("exited");
	}

	void OnTriggerStay()
	{
		theEnemyScript.BeenTriggered = true;
	}
}

In the Enemy script (probably badly named as it’s actually handling the guard logic) if DestroyEnemy != 0 && Time.time >= DestroyEnemy it sets BeenTriggered to false.

Well here’s the full script.

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	public GameObject EnemyPrefab;
	public Transform SpawnPoint;
	public Transform TargetPoint;
	public bool BeenTriggered;
	public float DestroyingEnemy = 0f;
	public GameObject WarningLight;

	private GameObject curEnemy;
	private float oneEveryXSeconds = 10f;
	private float newInstantiateTime;
	private GameObject newEnemy;
	private float enemySpeed = 2.0f;


	// Use this for initialization
	void Start () {
		newInstantiateTime = Time.time + oneEveryXSeconds;
		WarningLight.SetActive(false);
	}
	
	// Update is called once per frame
	void Update () {

		if (Time.time > newInstantiateTime)
		{
			newInstantiateTime = Time.time + oneEveryXSeconds;
			GameObject newEnemy = Instantiate(EnemyPrefab, SpawnPoint.position, SpawnPoint.rotation) as GameObject;
			curEnemy = newEnemy;
			curEnemy.transform.position = Vector3.Lerp(SpawnPoint.position, TargetPoint.position, Time.deltaTime * enemySpeed);
			//MoveObject(newEnemy, SpawnPoint, TargetPoint, 4.0);
		}

		if(curEnemy != null)
		{
			curEnemy.transform.position = Vector3.Lerp (curEnemy.transform.position, TargetPoint.position, Time.deltaTime * enemySpeed);
		}

		if(DestroyingEnemy != 0f && Time.time >=  DestroyingEnemy)
		{
			BeenTriggered = false;
			DestroyingEnemy = 0f;
		}

		if(BeenTriggered)
		{
			WarningLight.SetActive(true);
		}
		else
		{
			WarningLight.SetActive(false);
		}
	}
}