I have a player represented as a white circle here, that moves over a 8x15 grid of instantiated tiles. I’m trying to use Physics2D.OverlapCircle to compare the layer of the tiles the player collides with against the player layer. In the image below you can see Tile45 acts as expected but I would expect Tile60 to behave identically yet it isn’t caught until after Tile46 is registered. This problem continues with Tile 61 as we can see.
OverlapCircle only checks if some collider overlaps and it’s undefined which it returns if there are multiple. It can return the same collider over and over again even if there are other colliders overlapping.
Thanks for your response Adrian. I had it set to “All” before but was told:
Edit:
It does work when I go back to using All though. I guess its ok that I’m not reusing the list and its garbage collected in this context because I’m calling the function frequently right? I’m kind of doing my own clean up when I clear the list.
You pass it an array and it will populate the array with the results for you. You can reuse the same array every frame. Just make sure you provide an array big enough to capture the maximum number of overlaps you expect to happen, or you will miss some.
To note, all the NonAlloc and All suffixes are tentatively deprecated. All 2D physics queries without exception have overloads that return a single result or an array or List of results. Just look at the API docs without the above suffixes, you’ll see them all there.
I can’t seem to find any examples of the straight OverlapCircle returning a list of Collider2D objects. If I use the All version it works but when I don’t I get the problem described at the top of this post. Am I doing it correctly like this?
List.AddRange() should be used to add a collection of objects to another.
The method overloading is likely choosing OverlapCircle that returns one object to List.Add()
Edit:
Looking at the method overloads of OverlapCircle you need to use one that takes a list as extra argument for your desired use case. So not entirely interchangeable.
Hi, thanks for your comment. I understand that I need to use .AddRange now but I’m still unable to work out how to get OverlapCircle to return a list of Collider2D.
We don’t write methods from now on that produce garbage. Each time you call the All methods, the list is create and then left to be garbage collected. If you don’t care about this then go ahead and use it. A far better method is to reuse a list so you don’t get this.
Create a list and reuse it for your query. Pass it to the overload of OverlapCircle that requires a list. It’ll populate the list for you. Reuse that list and you’ll also get no garbage.