Search Unity

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

Discussion in 'Physics' started by benthroop, Oct 27, 2021.

  1. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    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.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    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.
     
  3. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    Ah ok, yes, that is fair/proper to iterate by hitcount. I just assumed that the array represented "what was hit". Fair enough!