- Home /
issues with on trigger enter and exit
So i have a seen set up where I have a flashlight object which you have to turn on and off to find clues, when you turn it on you have to leave the cone light trigger on the point of interest trigger for 5 seconds if you take it off before those 5 seconds it is meant to reset the timer, however if find when I move the flashlight trigger of manually it detects it and resets but when I turn the torch object off (which is what I intend for players to do) it doesn't recognize it as a trigger exit or anything, am I doing something wrong here? I am incredibly confused.
For a bit of clarity, I intend players to turn the torch off every time they stop using it and I want to make it so that the timer resets if you do, but the timer just keeps going anyway. ontriggerexit isn't working either is ontriggerstay.
edit:
The script is on the point of interest itself L_O_S is the line of sight of the torch.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Point_Of_Interest : MonoBehaviour
{
public GameObject Item_Dropped;
public float waitTime = 5f;
public bool isTriggered;
// Start is called before the first frame update
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "L_O_S")
{
Debug.Log("Object Triggered");
isTriggered = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "L_O_S")
{
Debug.Log("Object Lost");
isTriggered = false;
}
}
private void Update()
{
if(isTriggered == true)
{
waitTime -= Time.deltaTime;
}
if(isTriggered == true && waitTime <= 0f)
{
Debug.Log("Object found");
Destroy(gameObject);
}
if(isTriggered == false)
{
waitTime = 5f;
}
}
}
Answer by tormentoarmagedoom · May 14, 2019 at 07:57 AM
HEllo.
Post some code will help a lot :D Where is the script detecting the OntrIggeExit? Is in the torch? if is in the torch, is normal Ontriggerexit doesnt execute... the script is destroyed. (You can use OnDisable() and do exactly the same as OnTriggerEXit, this way when object is destroyed, its also called)
Bye!
OnTriggerExit is beeing executed? or its inside the method that something doesnt work?
it was neither, i've made it so that for a single frame the line of sight object is moved by 10000 on the z axis and then it runs the disable object code. the code now does what i want it to. thank you for your help though.