RaycastNonAlloc does not clear results array when hits == 0?

I was using RaycastNonAlloc to see if an object was hit. I looped over the results array to see if the expected object was there. To my surprise, if it was hit on one frame and was inserted into the results array, but then on the next frame was not hit, leaving the total hits to be zero, the results array still contained it!

To workaround I need to check for hits == 0 first, and assume that the object was not hit in that case. This is alright, but the behavior feels like a bug.

This is in 2019.4.18.

Why would that surprise you?

Clearing the array would be a complete waste which is why the number of hits populated into the array is returned. You don’t just look through the whole array looking for results, that would be odd. You would iterate only the hit count.

The whole point is that you reuse the array.

Ah ok, yes, that is fair/proper to iterate by hitcount. I just assumed that the array represented “what was hit”. Fair enough!