I came across a 2D video tutorial and there was a string:
Where i can find what type of value will return OverlapCircle? Is it bool, float or may be object?
In reference OverlapCircle is a Collider2D and this does not answer my question. Declaration of the OverlapCircle does not help me either and it is too complicated for my C# level.
From the script of video tutorial it is obvious that OverlapCircle will return Bool value. But how can i find out this myself with the only help of Unity documentation?
In the docs: Unity - Scripting API: Physics2D.OverlapCircle
It shows overlapcircle returns a Collider2D, with a little bit of extra info in the text:
If more than one collider falls within the circle then the one returned will be the one with the lowest Z coordinate value. Null is returned if there are no colliders in the circle
So it’ll return null if there’s no overlap, or the collider with the lowest z coordinate if there is one. From the collider you can do mycollider.gameobject to get the game object that it is part of, and from there you can access all its other parts, including the transform if you want info about its parent etc.
In other words, if you want to use it, the code would be something like:
//do the overlap test
Collider2d coll = Physics.OverlapCircle(circle_pos, circle_rad);
//check if we hit anything
if(coll != null)
//we hit something, so get the game object
GameObject collided_object = coll.gameObject;
//...do stuff with it here!...
If you check the docs a bit deeper you’ll also see you can specify a layer mask, minimum depth and maximum depth to filter objects, and also use OverlapCircleAll to get a list of overlapping objects, rather than just the one with the smallest z coordinate.