Collision inside a local function

Trying to find a way to detect collision with my box collider…
Things like Collider2D.Cast are not functional since they return the ammount of object collided and not a array/list with the id of the objects collided

Is there any OnCollisionEnter() that works inside a local function?
Or any solutions for that?

Collder2D.Cast does have a results array.


public int Cast(Vector2 direction, RaycastHit2D[] results, float distance = Mathf.Infinity, bool ignoreSiblingColliders = true);

You set the size of the results array yourself, it will be pushed through, and get filled out with what it hit. If it hit anything. Here is an example:


RaycastHit2D[] results = new RaycastHit2D[1];
int resultCount = collider.Cast(Vector3.down, results);

If it does hit more things than the size of the results array you created, they will be ignored.


oh ok

But is there anyhow to know the ammount of results that will be found without having to do the cast twice? (one for knowing the ammount and the other one for getting the arrays)