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. Dismiss Notice

OnTriggerEnter / OnColliderEnter is not working

Discussion in 'Scripting' started by heltonluizsb, Oct 12, 2019.

  1. heltonluizsb

    heltonluizsb

    Joined:
    Dec 20, 2018
    Posts:
    12
    I made 2 simple objects, marked um isTrigger and the other no. I made 2 codes, where:

    Trigger:
    Code (CSharp):
    1.  public class Gatilho : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.        Debug.Log("Deveria avisar");
    7.     }
    8.  
    9.     void OnCollisionEnter (Collision other)
    10.     {
    11.        if (other.gameObject.CompareTag("Player"))
    12.        {
    13.           Debug.Log("Colidiu");
    14.        }
    15.     }
    16. }
    Another object movement:
    Code (CSharp):
    1.  public class MOve : MonoBehaviour
    2.  {
    3.    // Start is called before the first frame update
    4.    void Start()
    5.    {
    6.    }
    7.    // Update is called once per frame
    8.    void Update()
    9.    {
    10.      transform.position = transform.position + new Vector3(0,0, -2f * Time.deltaTime);
    11.    }
    12.  }
    Everything very simple AND it doesn’t work…

    Below the Inspectors:

    Trigger:


    Another object:
     
  2. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    Which Unity version?
     
  3. cmyd

    cmyd

    Joined:
    Oct 29, 2017
    Posts:
    98
    Code (CSharp):
    1.  void OnCollisionEnter (Collision other)
    Is for Colliders,
    for triggers you need
    Code (CSharp):
    1.  void OnTriggerEnter(Collider other)
     
  4. MagyarPeter

    MagyarPeter

    Joined:
    Nov 28, 2020
    Posts:
    10
    The player needs a non-kinematic rigidbody to detect collisions.
    You don't have to use the rigidbody at all, so you don't need to change your player script.
    upload_2022-1-28_20-8-28.png