problem with OnCollisionEnter & Exit.

for some reason the debug code never activates:

using UnityEngine;
using System.Collections;

public class player_movement : MonoBehaviour {
    private Rigidbody2D player;
    private bool touching;
    void Start()
    {
        player = GetComponent<Rigidbody2D>();
        touching = true;
    }
    
    void OnCollisionEnter()
    {
        touching = true;
        Debug.Log("enter");
    }
    void OnCollisionExit()
    {
        touching = false;
        Debug.Log("exit");
    }
    void FixedUpdate ()
    {
        Debug.Log(Time.fixedTime + " " + touching);
        if (Input.GetButtonDown("Jump") && touching == true)
        {
            player.AddForce(new Vector2(0,15000));
        }
    }
}

all it ever displays is true. i made sure that both objects have 2Drigidbodys
and colliders but it still doesn’t work.

First of all make sure

  • You have a collider on both objects that should trigger the collissionEvent
  • At least one of them has a rigidbody attached

And declare the methods as follow

public void OnCollisionEnter(Collision collision) {
		touching = true;
		Debug.Log("Enter");
	}

	public void OnCollisionExit(Collision collision) {
		touching = false;
		Debug.Log("Exit");
	}

Felix