OverlapBox not working

I’m trying to detect all materials on an anvil for crafting with overlapbox (similar to codeMonkey’s tutorial) but it just doesn’t work. The overlap box does not detect any colliders. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Anvil : MonoBehaviour
{
    public BoxCollider collider;
    public LayerMask mask;
    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Fire2"))
        {
            CraftWithCollider(collider);
        }
    }
    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.matrix = transform.localToWorldMatrix;
        Gizmos.DrawWireCube(collider.transform.position + (collider.transform.rotation * collider.center), Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale));
    }
    private void CraftWithCollider(BoxCollider collider)
    {
        Debug.Log("Getting...");
        Collider[] colliders = Physics.OverlapBox(
            center:collider.transform.position + (collider.transform.rotation * collider.center),
            halfExtents:Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
            orientation:collider.transform.rotation,
            layerMask:mask
        );
        List<Transform> objectsInBox = new List<Transform>();
        List<string> ObjectsName = new List<string>();
        foreach (var c in colliders)
        {
            Transform t = c.transform;
            if(t.GetComponent<InventoryMine>() != null && t != collider.transform && !objectsInBox.Contains(t))
            {
                objectsInBox.Add(t);
                ObjectsName.Add(t.GetComponent<InventoryMine>().Me);
            }
        }
        for (int i = 0; i < objectsInBox.Count; i++)
        {
            Debug.Log(objectsInBox[i].GetComponent<InventoryMine>().Me);
        }
    }
}

I have tried using OnGizmosDraw to draw a wire cube where it should be overlapping, but I don’t understand matrix.
Any advice or suggestions would be appreciated.

Maybe enable gizmos and see if the box is actually colliding with the rock?

First check how many colliders are being detected. For example, you could print the length of the array:

private void CraftWithCollider(BoxCollider collider)
{
    Debug.Log("Getting...");
    Collider[] colliders = Physics.OverlapBox(
        center: collider.transform.position + (collider.transform.rotation * collider.center),
        halfExtents: Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
        orientation: collider.transform.rotation,
        layerMask: mask
    );

    print($"{colliders.Length} colliders detected");

    // ...
}

That version of OverlapBox (the “alloc” version) is not recommended if you need to call it all the time. Here’s the “non-alloc” version:

Collider[] colliders = new Collider[20];

private void CraftWithCollider(BoxCollider collider)
{
    Debug.Log("Getting...");
    int count = Physics.OverlapBoxNonAlloc(
        center: collider.transform.position + (collider.transform.rotation * collider.center),
        halfExtents: Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
        results: colliders,
        orientation: collider.transform.rotation,
        layerMask: mask
    );

    print($"{count} colliders detected");

    // ...
}

I will try this soon

It worked!!! Thank you very much!

but it only worked on sphere collider not my box collider, any idea why?