Collider isn't detected by Physics.Linecast

I’m currently working on a 3D, tile-based puzzle game. In this game the player controls a cube object and the animation it must use when moving is decided by a selectable object. One of these selectable objects is a teleporter and it’s the one that’s giving me trouble

The code that decides the cube’s animation is this one:

using System.Collections.Generic;
using UnityEngine;
public class TeleporterCube_SOLoading : CubeSOLoading
{
    public override void SetCubeTransformationSO()
    {
        for (int index = 0; cubeTransformationSO == null && index <= teleporterMaster.teleporter_Object_DetectionInstances.Count - 1; index++)
        {
            TeleporterObjectDetection teleporterObjectDetection = teleporterMaster.teleporter_Object_DetectionInstances[index];
            teleporterObjectDetection.GetCubeTransformationSO();
        }
    }
}

The teleporter is a parent object and, among their children, there are tiles. TeleporterCube_SOLoading calls GetCubeTransformationSO() on each teleporter tile until it gets an animation SO (Scriptable Object). The code goes as follows:

using UnityEngine;
public class TeleporterObjectDetection : ObjectDetection
{
    TeleporterMaster teleporterMaster;
    [Header("Z Axis")]
    public MovementDirection zMovementDirection;
    public Vector3 detectionRayOriginZ;
    public CubeTransformationSO zTransformationSO;
    public GameObject adjacentObjectZ;
    [Header("X Axis")]
    public MovementDirection xMovementDirection;
    public Vector3 detectionRayOriginX;
    public CubeTransformationSO xTransformationSO;
    public GameObject adjacentObjectX;
    void Start()
    {
        teleporterMaster = FindObjectOfType<TeleporterMaster>();
    }
    ...
    ...
    public void GetCubeTransformationSO()
    {
        if (adjacentObjectZ != null || adjacentObjectX != null)
        {
            Vector3 detectionLineEnd = Vector3.zero;
            if (adjacentObjectZ != null)
            {
                detectionLineEnd = adjacentObjectZ.transform.position + Vector3.up;
                if (Physics.Linecast(adjacentObjectZ.transform.position, detectionLineEnd, detectableLayers) == true)
                {
                    teleporterMaster.teleporterCube_SOLoading.cubeTransformationSO = zTransformationSO;
                }
            }
            else if (adjacentObjectX != null)
            {
                detectionLineEnd = adjacentObjectX.transform.position + Vector3.up;
                if (Physics.Linecast(adjacentObjectX.transform.position, detectionLineEnd, detectableLayers) == true)
                {
                    teleporterMaster.teleporterCube_SOLoading.cubeTransformationSO = xTransformationSO;
                }
            }
        }
    }
}

In the scene the cube is next to the teleporter. In this case, TeleporterObjectDetection.adjacentObjectZ is the tile on which the cube stands and it’s always detected, so all that’s left is to cast a line from there and detect the cube’s collider.

However, the cube’s collider is never detected. The cube has a collider component and it’s enabled, I’ve also made sure that it’s not a trigger. At first I thought that the linecast was missing it, so I used Debug.DrawRay to see the detection line, it clearly goes through it. I also stopped using a layer mask but it won’t detect it either, RaycastHit will always get a null collider too.

It’s true that the cube’s gameobject is disabled and it’s activated later but I’ve made sure that GetCubeTransformationSO() is only called once the cube has finished setting up. There aren’t any colliders between the cube’s collider and the linecast

I don’t know if this is related but I’ve found that GetCubeTransformationSO().detectionLine is not highlighted in Visual Studio, yet the code actually uses it? I’ve run the debugger and there’s clearly a new value

I really don’t know what I’m missing, any suggestions?

Okay, I’ve fixed it.

Turns out that the cube couldn’t be detected because the cube’s mesh was clipping thorugh the tile. I’ve simply moved the cube higher and it’s now detectable

I don’t understand why this is an issue, the colliders aren’t passing through each other. It works anyway, so I’ll take it

Can use Unity - Scripting API: Debug.DrawRay
and drawline to visualize linecasts (to see where it starts and ends)

From Project/Physics settings can enable: “Queries Hit Backfaces”
So raycast hits from inside the collider also, but maybe works on meshcollider only.