C# GameObject is not detecting collision with Character Controller

Hi everyone, I have a script that detects when a Character Controller collides with a gameobject. I have attached my script to the gameobject but the script isn’t detecting when I move the Character Controller into the gameobject. Any idea what is wrong with my script?

using UnityEngine;
using System.Collections;

public class PlayerDetect : MonoBehaviour {

    void OnControllerColliderHit(ControllerColliderHit playerCollider){
     if(playerCollider.gameObject.tag == "Player"){
      Debug.Log("Hit Player");
     }
   }
}

OnControllerColliderHit events are only sent to the CharacterController script. If you want to do something in the other object’s script, use SendMessage to call some specific function in it - for instance:

CharacterController script:

void OnControllerColliderHit(ControllerColliderHit hit){
  if (hit.normal.y < 0.9){ // filter out ground collisions
    // try to call PlayerHit in the other object
    hit.gameObject.SendMessage("PlayerHit", hit, SendMessageOptions.DontRequireReceiver);
  }
}

Other object’s script:

void PlayerHit(ControllerColliderHit hit){
  Debug.Log("Player Hit");
}

A somewhat faster alternative is to check for the specific PlayerDetect script and, if found, directly call PlayerHit (CharacterController script):

void OnControllerColliderHit(ControllerColliderHit hit){
  if (hit.normal.y < 0.9){ // filter out ground collisions
    // if the other object has PlayerDetect script...
    PlayerDetect otherScript = hit.gameObject.GetComponent<PlayerDetect>();
    if (otherScript){ // call its function PlayerHit
      otherScript.PlayerHit(hit);
    }
  }
}

NOTE: This event happens all the time due to collisions with the ground. In order to quickly filter out such collisions, a simple trick is to ignore collisions where the normal angle from the ground is too steep. In this example, only normal angles < 64 degrees (sin(64) = 0.9) are processed. This trick works fine because hit.normal is a normalized vector, and in this case its Y coordinate is numerically equal to the sine of the elevation angle.

If you want to have it on the object you run into, you can use OnCollisionEnter.