OnTriggerEnter / OnColliderEnter is not working

I made 2 simple objects, marked um isTrigger and the other no. I made 2 codes, where:

Trigger:

 public class Gatilho : MonoBehaviour
 {
    // Start is called before the first frame update
    void Start()
    {
       Debug.Log("Deveria avisar");
    }

    void OnCollisionEnter (Collision other)
    {
       if (other.gameObject.CompareTag("Player"))
       {
          Debug.Log("Colidiu");
       }
    }
 }

Another object movement:

 public class MOve : MonoBehaviour
 {
    // Start is called before the first frame update
    void Start()
    {
    }
   // Update is called once per frame
   void Update()
    {
      transform.position = transform.position + new Vector3(0,0, -2f * Time.deltaTime);
    }
 }

Everything very simple AND it doesn’t work…

Below the Inspectors:

Trigger:

Another object:

Hi Helton,

Could you please format your code to increase its readability? The images seem to be broken.


See also:

Done ^.^! Look if its better ^.^

So for when an object is marked as is trigger, you need to use OnTriggerEnter. Other than that the inspector gets cut off early so I can’t tell but make sure one of the objects have a rigibody attached to it otherwise it won’t work.

Preferably the player gets the rigibody if it has one try putting a debug.log into the collision method but outside of the if statement.

Much better. Thank you. :slight_smile:

@David_Lamonaca is right. It’s better to add a Rigidbody to moving objects. Also test the OnTriggerEnter method.

And add a Debug.Log outside the if-block of your OnCollisionEnter method. It might be that the method is getting called.

This topic was automatically closed after 13 days. New replies are no longer allowed.

Privacy & Terms