GameObjects randomly stops beeing filtered by Layer.

I’m making a tiny puzzlegame to learn Unity and I have stumbled on a small problem where a piece randomly stops working.

When a piece stops working if I move another piece the non-working piece starts working again. The same if I change the non-working pieces location in Sceneview during play it also starts working again. So as far as I can see there’s no change to the piece that should make it work or not work.

To detect what piece I’m clicking on I use the following code in Update:

if (Input.GetMouseButtonDown(0))
    {
        // So UI isn't click through.
        if (!EventSystem.current.IsPointerOverGameObject())
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            {
                hit.collider.gameObject.GetComponent<IUserClickable>().OnClick(hit.point.x, hit.point.z);
            }
        }
    }

ray is a Ray, hit is a RaycastHit

By inserting Debug.Log lines in this code it’s line 7 Physics.Raycast that stops returning true for this piece and therefore no clicks on it is registered. By inspecting the piece during play it has the right layer and collider (it’s a meshcollider) so it shouldn’t be filtered out, but it is.

I can’t figure out what is happening since if I click on another piece the non-working piece starts working again and I make no changes to it.

Any ideas what I’m doing wrong?

Put an else clause in there does a non-layer-masked raycast when the first one fails… is the ray perhaps hitting something else? It should ignore everything that doesn’t match your layerMask, but it might give you intel.

There are also issues with raycasting starting from within colliders (ie, starting from inside one), so perhaps something is getting in the way of where your camera is (which is where the ray starts from).

Did the else clause. When a piece stops working a ray from the camera hits the background behind the piece. So it seems it doesn’t even register that the piece is there. It’s strange since if I just click on another piece no changes are made to the nonworking piece but it starts working again.

I added a button to move the pieces by 0.01 in x direction. When a piece stops working and I use this it starts working again.

Perhaps there’s a hole or gap in the mesh, or some other issue that makes it fail in the MeshCollider context… perhaps try replacing the Mesh Collider with a more-generic collider, such as a BoxCollider or a SphereCollider sized to approximate the piece, see if that works better.

Using a BoxCollider gives the same results. It’s the randomness I don’t understand. Most of the time it works as it should, and then it decides not to.

This is what it looks like with lines drawn, if that helps understanding the problem. The blue lines are towards and through a piece that’s not working, the white lines are a working piece. The not working piece (blue lines) starts working after 1 click on the working piece (white lines).

The MeshCollider mesh is the topfacing mesh of the pieces, it’s 2 triangles created at runtime.

6653953--760315--RaycastError.png

Did you ever set the Z scale on any of these colliders to zero? This will definitely impact collisions negatively, and that applies from the collider all the way back up to the root of the hierarchy.

It’s easy to inadvertently set the Z scale to zero when you’re thinking 2D, such as by doing:

transform.localScale = new Vector3( Width, Height); /// TERRIBLE ERROR! Z scale is zero!

To fix, always supply the third Z argument as 1.0f.

No, I never touch the scale as far as I can see. The MeshCollider is only a plane of 2 triangles though if that is a problem? But I’ve also tried with a BoxCollider and that didn’t fix it either.

The project can be downloaded here: Dropbox - Error - Simplify your life if anyone is interested to have a look (Unity 2020.2.0f1). I’ve trimmed it down to 11 c# files. Any ideas what I’ve done wrong is welcome.

So I added a Rigidbody (Is Kinematic is selected) to the puzzle pieces which might haved solved the problem. Atleast it doesn’t seem to happen anymore, but I don’t understand why. Any ideas?