"OnTriggerExit" Help please

  1. I have a “OnTriggerExit” that once the event is triggered, It would remove that Unit from a List, And at the same time it will Invoke another function " Invoke(“UpdateTarget”, 0);".

However, It is Possible that there are multiple Units that can trigger “OnTriggerExit”, Would it be possible, to only Invoke " Invoke(“UpdateTarget”, 0);" , if the Unit that exited the Trigger was my Target?

  1. if the Unit gets destroyed, does that also count as an “OnTriggerExit”?
 private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Enemy")
        {
            enemyUnitsWithinVisualRange.Remove(other.gameObject);
         
            Invoke("UpdateTarget", 0);                        
        }

     
    }

Thanks In Advance

  1. You might need a new UpdateTarget() function that accepts the other.gameObject to see if that’s the one it was targeting.

  2. not sure, give it a try!That’s the beauty of Unity: you can test stuff SO easily and rapidly. Make a scene and a little script, set it up, then get it in the trigger area, and destroy it, see if you get the exit event.

Thanks for reply, I think, This May do the Job, Will give it a shot once I get back home “if (other.tag == “Enemy” && other == target)”

It didnt work, This also didnt work “if (other.tag == “Enemy” && other == target.gameObject)”

If i could understand exactly, something could happen perhaps :slight_smile:

I think I fixed it lol

 private void OnTriggerExit(Collider other)
    {
        Debug.Log("A");

        if (target != null && (Vector3.Distance(transform.position, target.transform.position) > range))
        {  
            target = null;
            Debug.Log("B");
        }

        if (other.tag == "Enemy" && target == null) 
        {
            enemyUnitsWithinVisualRange.Remove(other.gameObject);
           
            Invoke("UpdateTarget", 0);                         
        }

    }
1 Like

Cool work bro :slight_smile:

I Still need to know if the Unit gets destroyed, does that also count as an “OnTriggerExit”?

I need to test it, but I am so slow at testing things Like that lol, I guess I should Put an enemy on an invoke delayed timer to destroy itself lol