Question by
SQUARE-BADGER · Nov 11, 2016 at 09:16 PM ·
c#scripting problemcolliderscripting beginnertriggers
OnTriggerExit does not work
I have a cube object that when i press E it disappear and it create another object in the players camera, i made a pressure plate that interacts with any Trigger, when the cube is created and is touching the pressure plate that activates something, but when the cube is destroyed while inside the plates trigger zone the trigger Exit does not work at all but if the cube just exits the trigger and not destroying itself it works. please help the code for pressure plate
void OnTriggerEnter(Collider hit)
{
Door.SendMessage("Open");
buttonpressed.enabled = true;
AudioSource.PlayClipAtPoint(clip, transform.position);
hit.transform.parent = transform.parent;
}
void OnTriggerExit(Collider hit)
{
Door.SendMessage("Close");
hit.transform.parent = null;
Debug.Log("detach");
}
the cube code
public GameObject ThePlayer; public GameObject playercube; // Use this for initialization void Awake () {
ThePlayer= GameObject.Find("Player");
playercube = GameObject.Find("playercube");
}
// Update is called once per frame
void Update () {
}
void HitByRayCast()
{
playercube.SetActive(true);
Debug.Log("he took me boss!");
Destroy(gameObject);
}
Comment
When a object is destroyed there no Collider to exit the trigger, for this reason OnTriggerExit is not called for destroyed GameObjects.