Collider being hit, OnTrigger not being called (not a noob question)

I am trying to get a ray to trigger an OnTrigger event when it hits an object (to detect mouse clicks). There are hosts of threads asking about how trigger events work, and as far as I can tell, I’m doing everything correctly.

Here is what I have set up:

  1. My game object has an attached RigidBody and a Collider
  2. The Collider is set to be a Trigger
  3. In Project Setting → Physics, rays are *not configured to ignore triggers
  4. The collision matrix in the settings enables all collisions
  5. The collider’s parent object is not on the Ignore Trigger layer, nor is the ray (at least, I don’t think so).

I create the ray with the following code, in a script attached to the main camera:

if (Input.GetMouseButtonDown (0))
{
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     RaycastHit hit;
     Physics.Raycast (ray, out hit);
     if (hit.collider != null)
     {
          print(hit.collider.ToString ());
     }
}

When the user clicks on the object with the collider, the ray *does hit it. The print statement verifies that the ray is running into the collider as expected.

Here is a snippet of the script, attached to the same object as the collider, that should be called on the event:

void OnTriggerEnter(Collider other)
{
     selected = true;
     print ("In trigger.");
}

Any ideas? I’ve been beating my head on this one for a while now!

I’m not 100% on this but I don’t think a Ray technically enters the trigger as its never really “exists”, thus OnTriggerEnter is not called. Rather the ray is a function that simply returns what it hits. OnTriggerEnter is called when another trigger or collider intersects the trigger.

So I think you’ll want to get the object the ray hit and call a function on one of its components.

Hoes is correct, firing a ray at a collider wont trigger it as far im aware.

Based on your code, you could use OnMouseDown as an alternative.

Thanks for the feedback! I ended up writing some code in my game manager that handles selection, so I’ve sort of side-stepped the problem.

Looks like someone else came to the same conclusion: http://answers.unity3d.com/questions/48466/raycast-not-colliding.html

From that link, I generally recommend avoiding SendMessage as it requires reflection.