on collision enter not working

there is no debug message or anything
ontrigger is false on both
i have rigid body on both
collider on botb
made sure to spell it right 10 times
i even tried ontriggerenter still not working
when the cube collides nothing happens

void OnCollisionEnter(Collision col){
Debug.Log(“hit”);
}

One of the rigidbodies needs to be non-kinematic for it to work. Have you check that?
Also, check the collision matrix and see if the layers the objects are in can detect collisions with each other.

1 Like

yeah both are non kinematic and same layer which is default

your game is 3D, right?

yeah

What type of colliders are you using? Are you using a mesh collider or a regular one? Is any of the objects marked as static? Do they ever register a collision or they just fail sometimes? Does your script inherits from monobehaviour? Have you tried with other objects and other layers? Have you try to see if other functions like collision stay are at least called? Does the object collide to the other (not letting it pass or moving it if you try to go against it).

Try checking this table, I’m running out of ideas:

they r box colliders
yes it is monobehavior
no i didnt try other layers nor other collision
i tried once they let collide but i changed gameobjects now they pass
both cases didnt work tho

ok thanks for help :slight_smile:

If non works, try to give more info. Test it in a new empty scene, post images with your hierarchy and components, give more info on how you’re moving the objects during play mode. Maybe they’re beign moved too fast and Unity can’t compute it. There are phyisics options that can check faster collisions. If they’re not generating a reaction with each other, you should see why this is happening.

ok thanks i will try those.

I have a new test project in which I have a sphere with a rigidbody in a static world of box and mesh colliders.

If the sphere leaves the ground and hits another object on its way down it will not trigger OnCollisionEnter() again making it impossible to jump. I am logging the OnCollisionEnter() and OnCollisionExit() and it seems OnCollisionExit() is called more often than OnCollisionEnter(). In the image attached you can see the sequence of events. The sphere is resting on the collider, but OnCollisionEnter() has not been called.

This is Unity 2019.3.0f6.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleMoveRigidbody : MonoBehaviour
{
    private Rigidbody _body;

    private Transform _camerae;

    private Vector3 _startPosition;

    private float _force = 3.0f;

    private bool _canJump = true;

    void Start()
    {
        _startPosition = transform.position;
        _body = GetComponentInChildren<Rigidbody>();
        _camerae = FindObjectOfType<Camera>().transform;
    }


    void Update()
    {
        if (_canJump)
        {
            Vector3 direction = (_body.position - _camerae.position).normalized;
            _body.AddForce(direction * (Input.GetKey(KeyCode.W) ? _force : Input.GetKey(KeyCode.S) ? -_force : 0.0f) + Vector3.Cross(Vector3.up, direction) * (Input.GetKey(KeyCode.D) ? _force : Input.GetKey(KeyCode.A) ? -_force : 0.0f), ForceMode.Force);
            _body.AddForce(Vector3.up * _force * (Input.GetKeyDown(KeyCode.Q) ? _force : 0.0f), ForceMode.Impulse);
        }
        else
        {
            _body.AddForce(Vector3.up * _force * (Input.GetKey(KeyCode.E) ? -_force : 0.0f), ForceMode.Impulse);
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            _body.velocity = _body.angularVelocity = Vector3.zero;
            _body.position = _startPosition;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        _canJump = true;

        Debug.Log(_canJump + " | " + collision.collider.name);
    }

    private void OnCollisionExit(Collision collision)
    {
        _canJump = false;

        Debug.Log(_canJump + " | " + collision.collider.name);
    }

    private void OnCollisionStay(Collision collision)
    {
        //Debug.Log(collision.collider.name);
    }
}

Please point out what I am doing wrong.

I have a workaround here:

    private void OnCollisionEnter(Collision collision)
    {
        ++_collisions;
    }

    private void OnCollisionExit(Collision collision)
    {
        --_collisions;
    }

It checks if _collisions is over 0 to allow movement.

Could also compare colliders to a cached collider in OnCollisionExit() to see if they are different.

I think it could be improved with the option of having the events be ordered so that upon leaving an object’s collider it will be followed up by OnCollisionEnter again to make sure it is always the last event called if true for at least one object. Using OnCollisionStay() is costly for performance and I would rather avoid using that.

yah i have the same problem someone can tell me what is wrong in my scripts ?(im begginer)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collision : MonoBehaviour
{
 
    void OnCollisionEnter(Collision other)
    {
       if (other.gameObject.CompareTag("barrier"))
        print("re");
    }
}

first of all my case was that i imported an asset that added tags called collide and all objects haf to have so oncollison can be called so please check for this

is this script sitting on the object that has the collider?

I also had this problem, but I could find my emberassing error. I could not see the messages, because I deactivated the Unity messages in the console:sweat_smile:. Look if the buttons of the top-right corner are activated.

im having the same exact issue here’s my code

private void OnCollisionEnter(Collision Collision);

I literarily started like yesterday so I’m very very confused

Please don’t hijack old threads. It’s against forum rules. Instead, create your own new thread… it’s FREE!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand errors in general:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379