Home > OS >  Unity, how to access correct declaration in the API?
Unity, how to access correct declaration in the API?

Time:11-08

I have been using the Physics2D circle cast and taking a look at the documentation, https://docs.unity3d.com/ScriptReference/Physics2D.CircleCast.html , there are 3 declarations of the same name, and the third one fits my needs best, how do I use this one rather than the first one?

Physics2D.CircleCast(transform.position, radius, Vector2.up, ContactFilter2D.NoFilter, colliders, 0f);

This is what I have so far and it is giving me a compiler error saying that the contact filter needs to be an integer.

CodePudding user response:

What you are talking about are Overloads.

An overloaded method means you have multiple versions with same name but different signatures.

=> You don't actively decide which overload you use!

The compiler itself figures out which parameters of which types you provide in which order and automatically uses the method overload with the signature that best fits the given parameters.

So if you write

Physics2D.CircleCast(transform.position, radius, Vector2.up, new ContactFilter2D().NoFilter(), colliders, 0f);

it will automatically use the overload with signature (if your colliders is a List<RaycastHit2D>)

Physics2D.CircleCast(Vector2, float, Vector2, ContactFilter2D, List<RaycastHit2D>, float);

or accordingly (if your colliders is a RaycastHit2[])

Physics2D.CircleCast(Vector2, float, Vector2, ContactFilter2D, RaycastHit2D[], float);
  • Related