Why does my Physics.Raycast not hit my BoxCollider?

I have a panel in my UI. It is actually a box, which is a subcomponent of a prefab, which I instantiate from script at a position in front of the camera (and under it hierarchically). The box has a BoxCollider component. I can see in the scene view that the collider matches the box itself. When the screen is tapped, I’m doing this:

        var ray = Camera.main.ScreenPointToRay (screenPos);
        RaycastHit hit;

        print ("InfluenceEnvironment: check hit");
        if (Physics.Raycast (ray, out hit, 500f)) // Note that the panel's distance from the camera is less than 500, and I have tried specifying no distance and thus casting an infinite ray, but the effect is identical.
        {
            print (string.Format("InfluenceEnvironment: Got hit: {0}", hit.transform.gameObject.name));
            Debug.DrawRay(ray.origin, ray.direction*2000, Color.magenta, 10);
                        ...
        }

If I touch the middle of my panel, all is fine. But if I click the edge, it doesn’t work. I can see in the scene view that the ray from Debug.DrawRay() is passing through the collider in question. Yet the object in the RaycastHit is the terrain, which lies behind the box. And, bizarrely, if I change the collider at all in the inspector (e.g. changing x pos by 0.0001), it starts working (but making the equivalent change from code does not help). Simply disabling / re-enabling the collider also has this effect of fixing it. I’ve tried changing to a capsule collider instead, just to see it that works, but also have the same problem.

I also see that if I add the prefab to the scene manually instead of in script, that also works.

I wonder if the problem is related to the one discussed in http://forum.unity3d.com/threads/572…-objects/page2. However, I don’t think so, as it persists even when none of the objects involved nor the camera move.

I am using Unity 4.0.1f2

Any thoughts / workaround would be welcome! Regards,

Andy

I found a workaround for this. Immediately after creating my panel, I do:

		textPanelObj.SetActive(false);
		textPanelObj.SetActive(true);

Previously I had tried disabling / enabling the collider associated with the object, but not the object itself. This seems to resolve the problem.

Simply disabling / re-enabling the collider also has this effect of fixing it.

Isn’t this your answer?
If that fixes your problem why not grab the collider component and re-enable it upon instantiating it?
It could simply be a bug with unity when instantiating object, that needs to be fixed and up untill then you need to use this workaround?

If this is really a “UI box” which is being moved around to always occupy the same area on the screen, could just check screen coords directly, with no ray cast.

But, I’m wondering if the box is very close to the camera, and the rayCast is starting inside the box, so is ignoring it. ScreenPoint to ray starts NearPlane away from the camera (can print ray.origin.) If that was the case, could adjust the ray to start further back: ray.origin -= ray.direction * 2;

I am having the same issue in Unity 5.0.0f4

I have a moving platform that the player jumps on. The player object casts a ray to see if it has landed on something.

  1. When the platform is placed in the scene, in the inspector there are no problem. The player lands and the raycast reports landing.
  2. When the platform is instantiated through script the rays pass right through it.
  3. Resetting (disable / enable) the game object or the collider did not help me.
  4. I found that if I move the platform down 0.04f that it again begins to get hit by the raycasts

The solution to my problem was to add a script to the platform’s Start() AND I had to constrain the y-axis movement on the rigidbody.

        Vector3 posReset = this.transform.localPosition;
        posReset.y -= 0.04f;
        this.transform.localPosition = posReset;