Problem OnCollisionEnter function

Hi everyone!:

I have a pattern “state” for AI behaviors.

In one of the scripts as part of the pattern “state” (call for example stateFollowTarget) I have placed the function OnCollisionEnter () to detect if it hits the target and pass a bomb in case there is a collision.

I’m surprised the OnCollisionEnter () function is not executed within that script and I don’t know why.

To check if the function was executed, I placed a Debug.Log(“Collision detectada”); But this message never appears on console even when a collision. It means that the function is not executed.

I tried to place the function (whithout changing the code that is inside), in the “main script” using the interface states and that it works. But not in the script that I need to works.

The script in which I need to work inherited from the interface AI_States

public class statePerseguirObjetivo : IA_State {…}

The first thing I think is that also inherits from MonoBehaviour as AI_State is an interface, not a class. But OnCollisionEnter () function does not run.

public class statePerseguirObjetivo : MonoBehaviour, IA_State {

void OnCollisionEnter(Collision col)

{

Debug.Log(“Collision detectada…”);

}

}

Anyone know why this happens? Anyone know how to fix it?

A greeting. Thanks foward.

First, narrow it down to a collision issue (that is, something incorrectly configured in your colliders) versus an inheritance/interface issue. If you remove the IAState interface from the class definition, is the collision triggered then?

Thank you for your reply.

To prove what you say me, I would have to remove the pattern state and program differently. So the whole design of code that have implemented would be meaningless.

I don’t think that’s it. I’ve changed the interface and have become abstract class and function OnCollisionEnter() still does not run in the FollowTargetState class…I have done this:

public abstract class  IStates_IA_CarStates : MonoBehaviour
// before it was public Interface IState_IA_CarStates{....}

And now FollowTargetState class inherits from IState_AI_CarStates:

public class FollowTargetState : IState_IA_CarStates
{
  ...
  void OnCollisionEnter(Collision col)
  {
  Debug.Log("Collision detected..."); //Never run this Debug.Log
  }
  ...
}

Why can this be happening? Thanks again in advance.