Search Unity

[SOLVED]OnTriggerEnter Not Working: Even With Rigidbodies, IsTrigger, and Colliders?

Discussion in 'Editor & General Support' started by Skitto, Jun 5, 2017.

  1. Skitto

    Skitto

    Joined:
    Jun 3, 2017
    Posts:
    19
    Hello. I'm fairly new and have been trying to get an enemy to attack my player when the player is in range of a sphere collider. Unfortunately, collisions for OnTriggerEnter and OnTriggerExit are not registering.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyAttack : MonoBehaviour {
    6.  
    7.     public float period = 0.5f;
    8.     public int attackDamage = 10;
    9.  
    10.     Animator anim;
    11.     GameObject player;
    12.     HealthControl playerHealth;
    13.     EnemyHealth enemyHealth;
    14.     bool playerInRange;
    15.     float timer;
    16.  
    17.  
    18.     void Awake ()
    19.     {
    20.         player = GameObject.FindGameObjectWithTag("Player");
    21.         playerHealth = player.GetComponent<HealthControl>();
    22.         enemyHealth = GetComponent<EnemyHealth>();
    23.         //animator
    24.     }
    25.    
    26.     void onTriggerEnter(Collider other)
    27.     {
    28.         if(other.gameObject == player)
    29.         {
    30.             Debug.Log("InRange");
    31.             playerInRange = true;
    32.         }
    33.  
    34.     }
    35.     void onTriggerExit(Collider other)
    36.     {
    37.         if(other.gameObject == player)
    38.         {
    39.             playerInRange = false;
    40.             Debug.Log("NotInRange");
    41.         }
    42.     }
    43.  
    44.     void Update ()
    45.     {
    46.         timer += Time.deltaTime;
    47.  
    48.  
    49.         if(timer >= period && playerInRange && enemyHealth.currentHealth > 0)
    50.         {
    51.             Attack();
    52.         }
    53.     }
    54.  
    55.     void Attack()
    56.     {
    57.         // Reset the timer.
    58.         timer = 0f;
    59.  
    60.         Debug.Log("EnemyAttack");
    61.  
    62.         // If the player has health to lose...
    63.         if (playerHealth.currentHealth > 0)
    64.         {
    65.             // ... damage the player.
    66.             playerHealth.TakeDamage(attackDamage);
    67.         }
    68.     }
    69. }
    Above is the code for my attack script. I'm using a modified version of the Unity tutorial one. However, not even the Debug.Logs are showing up, meaning that a collision must not be registering.




    Here are the two objects that should interact. Both have rigidbodies and colliders. The enemy has a IsTrigger spherical collider along with its capsule collider. However, there's no collisions.

    I've researched for a while, and it seems most problems are due to a lack of rigidbodies or IsTrigger not being checked, or colliders, but these are already covered. However, it is still not working. I'm not sure if I'm missing some small bit of information, or missed a tiny detail I had to check.
     
  2. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    The mech* should have a collider on it.
    The cylinder should have a collider on it, and it should be marked "isTrigger"

    Next, there is an array of "what collides with what" in they physics manager, make sure the layers are selected to collide with each other.

    *originally I thought it was a mouse! haha.


    Edit: Also, you have "OnTriggerEnter" with the wrong case, needs the initial "O" capitalized.
     
    Last edited: Jun 5, 2017
    Kadhemi, ps2goat, shaozeZhou and 4 others like this.
  3. Skitto

    Skitto

    Joined:
    Jun 3, 2017
    Posts:
    19
    I was right when I was thinking that I was getting something very small and very stupid wrong...

    I didn't capitalize the O's. That was all that was wrong.

    Thanks for the help XD
     
    ps2goat and L_Dude like this.
  4. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    You're welcome! I almost didn't catch it, it looked right at first glance. :)
     
    bharathasl74185 and Kadhemi like this.
  5. scarofsky

    scarofsky

    Joined:
    Sep 29, 2016
    Posts:
    11
    Wow, the exacly same reason as you...
     
  6. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    @Jaimi I have two spheres with RGBs, colliders, on same layer, each with OnCollisionEnter in the single script on both GOs.
    I created a third object duplicated from one of the spheres.
    I run the game and move the 3rd GO to create an impact yet there is no debug.log report. The RGBs do bounce off each other.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class HandedCollision : MonoBehaviour
    7. {
    8.     public GameObject m_leftCollider;
    9.     public GameObject m_rightCollider;
    10.     public GameObject m_TmLeft;
    11.     public GameObject m_TmRight;
    12.     private TextMeshProUGUI lefttextmesh;
    13.     private TextMeshProUGUI righttextmesh;
    14.     private int leftcollisionCount = 0;
    15.     private int rightcollisionCount = 0;
    16.  
    17.     void OnCollsionEnter(Collision col)
    18.     {
    19.         Debug.Log("incoming collision: "+ col.gameObject.name);
    20.         switch (col.gameObject.name)
    21.         {
    22.             case "Lefty":
    23.                 ++leftcollisionCount;
    24.                 lefttextmesh.text =
    25.                         "<b><u><#ffff00>" + col.gameObject.name +
    26.                         ":</color></u></b>" + "\n" +
    27.                         "Collision: " +
    28.                         leftcollisionCount + "\n";
    29.                 break;
    30.             case "Righty":
    31.                 ++rightcollisionCount;
    32.                 righttextmesh.text =
    33.                         "<b><u><#ffff00>" + col.gameObject.name +
    34.                         ":</color></u></b>" + "\n" +
    35.                         "Collision: " +
    36.                         rightcollisionCount + "\n";
    37.                 break;
    38.  
    39.             default :
    40.                 ++leftcollisionCount;
    41.                 lefttextmesh.text = ":" + col.gameObject.name + ": " + leftcollisionCount;
    42.                 ++rightcollisionCount;
    43.                 righttextmesh.text = ":" + col.gameObject.name + ": " + ++rightcollisionCount;
    44.                 break;
    45.         }
    46.     }
    47.  
    48.     // Start is called before the first frame update
    49.     void Start()
    50.     {
    51.         lefttextmesh = m_TmLeft.GetComponent<TextMeshProUGUI>();
    52.         lefttextmesh.text = "<b><u><#ffff00>LeftHandedCollider</color></u></b>";
    53.         righttextmesh = m_TmRight.GetComponent<TextMeshProUGUI>();
    54.         righttextmesh.text = "<b><u><#ffff00>RightHandedCollider</color></u></b>";
    55.     }
    56. }
    57.  
    58.  
    59.  
    This should be simple and is straight out of the demos I have seen. I kept it simple for just this reason
    Thanks in advance.
     
  7. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    I guess I had to go through this exercise to find the typo: OnColl!!!!!!!!!!!!!!sion. Argh
     
  8. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    I had a similar problem with OnTriggerEnter(Collider other)
    i used:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.       if (other.tag == "MYCOLLIDERTAG")
    4.       {
    5.           //...
    6.       }
    7. }
    The collision was detected in the editor, but NOT in in builds!

    The PROBLEM are the TAGS!
    If you go to Project Settings -> Tags and Layers -> under Tags are (Removed!) items.
    All items after the (Removed!) item don't work in Builds.

    The SOLUTION was to close Unity and open the project again.

    It seems the player can't handle this. The Editor can.
    And the real problem was not the collision, it was the Tag. (Unity 2019.2.0f1)
     
    Last edited: Aug 8, 2019
    Charan_PSR likes this.
  9. Chobrenga

    Chobrenga

    Joined:
    Aug 3, 2020
    Posts:
    3
    I wanted to add for anyone else having this issue, make sure you're using the right version of OnTriggerEnter(). There is a 2D version : void OnTriggerEnter2D(Collider2D other)
     
  10. catcarrot

    catcarrot

    Joined:
    May 3, 2016
    Posts:
    1
    thank you, that was exactly the issue I had. I was using Collider2D
     
  11. bobby55

    bobby55

    Joined:
    Jun 24, 2020
    Posts:
    48
    thanks also Chobrenga!
     
  12. abitowhit

    abitowhit

    Joined:
    Aug 9, 2020
    Posts:
    13
    Seriously?!.. I did the same "o"ld thing. Unberieeevable!
    Cannot believe I did not notice it right away. and thanks to Jaimi!
     
  13. AmirSavand

    AmirSavand

    Joined:
    Oct 17, 2017
    Posts:
    1
    For those who spend time with stupid mistakes, typos, etc. Just use Rider.
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    And to have a brief look at the available forums and try to post in the correct one where appropriate as you'll get better feedback. The physics forum is here.
     
  15. CranberryJuice-

    CranberryJuice-

    Joined:
    Aug 19, 2020
    Posts:
    30
    I love rider but I can barely pull out 13.90 dollars a month.
     
  16. Kadhemi

    Kadhemi

    Joined:
    Aug 13, 2021
    Posts:
    1
    god bless you Jaimi