list.Count

Hello,

Collider[] range_checks = Physics.OverlapSphere(transform.position, radius, player_layer);

In a video tutorial I followed it was:

if(range checks.Length != 0)
{
...
}
if(range_checks.Count != 0)
{
(...)
}

But neither of those work. For .Length there’s no error and it’s doesn’t work. For .Count it says ‘Collider[ ] has no definition for Count’

So, yeah that’s it, hope that you show me the solution!

Did you mean range_checks for the first example?

For both.

Put it in a debug log and see what’s in the array. Maybe it’s just empty

1 Like

Smart thinking there, cowboy. It’s actually empty. Here, I got another question for you smartboy. What to do with it, now that we know it’s empty.

What is your range? Is the player_layer accurate? Do the objects you’re detecting have colliders?

So, range_checks is a list of objects on the “player” layer. It’s an enemy script and because there’s only one player there could only ever be one item in the list. I don’t understand what it means for a layer to be accurate and yes player has a capsule collider. Thanks for any future help!

Alright so rather than use an expensive operation like OverlapSphere, why not keep a reference to the Player and check Vector3.Distance?

Just followed a tutorial and this problem came up. Thanks for the solution, will try. Wil hit you with the results as soon as I do it.

Well, “player_layer” should be a layer mask, not a layer index. So what exactly is player_layer? What’s its value? We can only assume things based on what we can see here. We don’t know how this variable is declared or what values it may contain. We can tell you that the method OverlapSphere does work as expected. So if you don’t get a result, you’re either checking the wrong layer(s), your range is smaller than the distance to the object you want to find or the provided position is not what you may expect.

Other possible issues are that your target object does not have a collider or does have a wrong collider (2d physics collider instead of 3d physics collider?)

1 Like

It works now, after doing it with Vector3.Distance.