- Home /
Trigger and collision events not being called
I have two objects that I would like to know when they collide. As a disclaimer I have read the documentation several times and am currently following what is says precisely, or so I think. Inspector views of the objects are included below.
The object I'm checking the collision against is a cube with a non-kinematic rigidbody and a box collider. The other object is a plane with a box collider, that is marked as a static object. Both are on the Default physics layer. OnCollisionEnter and OnTriggerEnter are both not being called, not matter if the plane is marked as a trigger or not.
Here are both scripts:
cube
using UnityEngine;
using System.Collections;
public class BoxScript : MonoBehaviour {
public enum BoxTypes
{
BOX_1X1,
BOX_1X2,
BOX_1X4
}
public enum BoxColor
{
BLUE,
GREEN,
RED,
YELLOW,
ORANGE,
WHITE
}
/*****************************************
* Public Variables*/
public BoxTypes m_boxType = BoxTypes.BOX_1X1;
/*****************************************
* Private Variables*/
private bool m_isSelected;
private BoxColor m_color;
/// <summary>
/// Property for setting/getting selected boolean
/// </summary>
public bool IsSelected
{
get
{
return m_isSelected;
}
set
{
m_isSelected = value;
}
}
/// <summary>
/// Initialization
/// </summary>
void Start ()
{
m_isSelected = false;
m_color = DetermineColorByMaterial();
}
public BoxColor GetColor()
{
return m_color;
}
private BoxColor DetermineColorByMaterial()
{
string materialName = renderer.material.name;
char[] delims = {'_'};
string[] colorNames = materialName.Split(delims);
if (colorNames.Length > 1)
{
string color = colorNames[colorNames.Length - 1];
if (color == "Blue")
{
return BoxColor.BLUE;
}
else if(color == "Green")
{
return BoxColor.GREEN;
}
else if (color == "White")
{
return BoxColor.WHITE;
}
else if (color == "GOLD")
{
return BoxColor.YELLOW;
}
}
return BoxColor.ORANGE;
}
void OnTriggerEnter(Collider collider)
{
Debug.Log("trigger hit!");
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("Collision happened on box!");
}
}
plane
using UnityEngine;
using System.Collections;
public delegate void CollisionDetectedEventHandler(BoxScript.BoxColor boxColor);
public class SwitchTarget : MonoBehaviour
{
public CollisionDetectedEventHandler OnCollisionWithBlock;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider collider)
{
Debug.Log("Trigger has been entered");
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("Collision happened!");
//did we collide with a box?
if (collision.gameObject.GetComponent<BoxScript>() != null)
{
//now get the color of the box
if (OnCollisionWithBlock != null)
{
OnCollisionWithBlock(collision.gameObject.GetComponent<BoxScript>().GetColor());
}
}
}
}
The test case I am using is the cube falling onto the plane when the scene starts. I'm wondering if for some reason the rigidbody on the cube is sleeping, but as soon as it hits the plane it should wake up.
A simple package with the problem has been included. Any help would be greatly appreciated.
Please don't post answer as a comment or additional info
Answer by Programmer-Tau · Nov 02, 2013 at 04:22 AM
I just tried your package. OnCollisionEntered is being called. So... it works on my machine.