Need help with Physics.OverlapSphere.

Hey,
I need help with a piece of C# script that i can’t wrap my head around.
I’ve been staring at this for hours now and i can’t get it to work.
What it needs to do basically:
I have a trigger which is in a steady spot.
This trigger needs to detect if a gameobject is near it.
When a specific gameobject is near it named per example CubeOne.
Then it needs to do something (transform the position of CubeOne).

This is part of the script i wrote so far but it doesn’t seem to work.
The Debug.Log is in there to see if it does anything.

public Vector3 stuff;
public float radius = 2f;
public int LMask = 1 << 10;

void Update()
{
	Collider[] colliders = Physics.OverlapSphere(transform.position, radius, LMask);
	foreach (Collider col in colliders)
	{
		if(gameObject.name == "CubeOne")
		{
			Debug.Log("HittingBox");
			stuff = col.transform.position;
		}
	}
}

Please help. I’m very new in scripting.

Your code is fine. Your method is fine. I see no reason why this shouldn’t work, assuming your layer mask is doing what you think it’s doing.

…and therein lies 99% of the problems people have with Physics.(anything).
The layer mask is tricky and irritating, because, let’s face it, the bit shift operator makes little sense and I started coding in c when I was a little girl. (I was a weird little girl) I understand the concept and how it works, but I only learned it because someone put a gun to my head and forced me to use the bit shift operator in a problem set. (I get kidnapped by computer science students a lot)

Anyway, I would put good money on your problem being the layer mask.
If you are sure it’s not the layer mask, then you can come yell at me, but I am writing this on a google chrome window while procrastinating solving a layer mask problem on my own Unity project.

If instead of setting it as an int, you do…
public LayerMask LMask;

You can use the inspector to check and uncheck boxes. If you are thinking “But I know what I’m doing! I am sure this layer mask is doing what I think it’s doing!”, then you are either mistaken or a computer scientist.

If this object that comes close is not dynamic and does not change (from what I understand from your question) then why not just store it as a transform and use Vector3.Distance to check?

public Transform cubeOne;
public float triggerDistance = 1.0f;

void Update(){
    if(Vector3.Distance(transform.position, cubeOne.position) <= triggerDistance){
        //do whatever
    }
}

Also, the way youre using layermask seems more complicated than it needs to be

public LayerMask mask;

and in the editor, just select the layers in the dropdown.

@oswin_c beat me to the layermask comment by 1 minute!

Another thing to keep in mind…rigidbodies. You need a rigidbody on the object you wish to detect. Another “gotcha” is if you have a parent object with a rigidbody on “LayerA” and you want to detect a child of that object that is on “LayerB”, that child object will need its own rigidbody.

if (gameObject.name == “CubeOne”) is wrong
it should be: if (col.gameObject.name == “CubeOne”