Cannot create a 2D collider array
Hi, I'm having an issue where an error comes up saying "Cannot implicitly convert type 'UnityEngine.Collider2D' to 'UnityEngine.Collider2D[]'". I'm not sure how to resolve this, but after some reading I'm sure it has something to do with Lists, which is something I'm not familiar with yet (still learning programming).
The effect I'm attempting to create is a blowback effect (like an explosion pushing back entities around it), but only the Y-axis is affected. Haven't been able to test if the effect works yet, due to this error. Thank you for anyone who's able to provide advice.
void BlowbackArea(Vector2 location, float blowbackRadius, float hpmMagnitude)
{
Collider2D[] objectsInRange = Physics2D.OverlapCircle(transform.position, 6.5f, whatIsHexPlate);
foreach (Collider2D col in objectsInRange)
{
HexPlate hexPlate = col.GetComponent<HexPlate>();
if (hexPlate != null)
{
float proximity = (transform.position.y - hexPlate.transform.position.y);
float effect = 1 - (proximity / blowbackRadius);
}
}
}
Answer by hexagonius · Oct 25, 2016 at 08:31 AM
Physics2D.OverlapCircle only returns one Collider2D. That's why the error. Use OverlapCircleAll, which returns the desired array of Collider2Ds, which is probably what you intended to do
Answer by MelvMay · Oct 25, 2016 at 09:56 AM
Better still use the non-allocating version OverlapCircleNonAlloc. Note you need to allocate an array that's large enough for the number of results you expect and pass that.
Answer by FlyingFlambe · Oct 25, 2016 at 05:57 PM
Thanks @hexagonius and @MelvMay. I should've looked into it further ahh D: So simple. Much appreciated.
Answer by FunkyPhoenixGames · May 07, 2018 at 09:49 AM
Wow what a coincidence... It just so happens that I was trying to do the same thing (an explosion affect) but I encountered this error! Thank you for helping me out @hexagonius and @MelvMay :)
Your answer
Follow this Question
Related Questions
Do I need Rigid2DBody if I don't plan to use physics? 2 Answers
Problems using RaycastHit2D.collider, detection only on the rays origin 1 Answer
How do I Implement Linecast2d 0 Answers
2d collisions physics not working when using mouse as input 0 Answers
Physics 2D disable collision bouncyness 0 Answers