Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnTriggerEnter and OnTriggerExit (Exit never gets put to work)

Discussion in 'Scripting' started by RobLande, Jun 19, 2017.

  1. RobLande

    RobLande

    Joined:
    May 3, 2017
    Posts:
    7
    Hi :>

    I want a car to move to point A, but when the car comes in touch with a Tank object I want the car to stop moving to point A, and instead I want it to follow the Tank object he came in touch with. I made the Box Collider Is Trigger.

    Code (CSharp):
    1. void OnTriggerEnter(Collider col)
    2.     {
    3.         if (col.gameObject.tag == "Faction_A_Tank")
    4.         {
    5.             colWithEnemy = true;
    6.         }
    7.     }
    8.  
    9. void OnTriggerExit(Collider col)
    10.     {
    11.             colWithEnemy = false;
    12.     }
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         if (colWithEnemy == true)
    4.         {
    5.             FollowEnemyTank();
    6.         }
    7.         if (colWithEnemy == false)
    8.         {
    9.             MoveToObjective();
    10.         }
    11.     }

    This works up to the point when the Tank gets destroyed or I deleted the Tank in the Hierarchy. When the Tank is destroyed, colWithEnemy never gets set to false. Somehow the OnTriggerExit never gets working?

    Ive been struggling for this for hours now, tried OnTriggerStay, OnCollisionEnter, Exit and Stay. And tried every possible combination there is of Enters Exits and Stays.


    Thank you for your time
    Rob
     
    Last edited: Jun 19, 2017
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Please you code tags and your OnTriggerExit doesn't take any parameters, even if you aren't using it, you still need the collider parameter.
     
  3. RobLande

    RobLande

    Joined:
    May 3, 2017
    Posts:
    7
    Code (CSharp):
    1. void OnTriggerExit(Collider col)
    2.     {
    3.             colWithEnemy = false;
    4.     }
    Same effect, they work fine until the Tank gets destroyed, and the colWithEnemy never gets set to false.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Then set it to false wherever your destroy the tank.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Yeah, OnTriggerExit only happens when a collider exits the trigger. A collider already inside the trigger getting destroyed/deactivated doesn't cause an OnTriggerExit.
     
    jacobrutter likes this.
  6. RobLande

    RobLande

    Joined:
    May 3, 2017
    Posts:
    7
    Yes I am finding out right now, if I move the box collider of the tank away from the car then the colWithEnemy gets set to false. Maybe it's better to use RayCasts instead of Triggers for this problem?
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    You could replace the bool with a reference to the tank, and then check if the tank still exists (!= null):

    Code (CSharp):
    1. void OnTriggerEnter(Collider col)
    2.     {
    3.         if (col.gameObject.tag == "Faction_A_Tank")
    4.         {
    5.             followingTank = col.gameObject;
    6.         }
    7.     }
    8.  
    9. void OnTriggerExit(Collider col)
    10.     {
    11.         if(col.gameObject == followingTank)
    12.             followingTank = null;
    13.     }
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         if (followingTank != null)
    4.         {
    5.             FollowEnemyTank();
    6.         }
    7.         else
    8.         {
    9.             MoveToObjective();
    10.         }
    11.     }
     
    RobLande likes this.
  8. RobLande

    RobLande

    Joined:
    May 3, 2017
    Posts:
    7
    Great thinking! It works perfectly! And now with multiple Tanks I replaced OnTriggerEnter to OnTriggerStay and it works like a charm. Thank you so much!