Search Unity

This method will be deprecated in a future build - why?

Discussion in 'Editor & General Support' started by Seanchaoz, Jun 28, 2022.

  1. Seanchaoz

    Seanchaoz

    Joined:
    Jun 19, 2019
    Posts:
    18
    So I'm seeing this "This method will be deprecated in a future build" every now and then in the Unity documentation. Specifically in this case in the BoxCastNonAlloc (Circle and Capsule as well).

    https://docs.unity3d.com/ScriptReference/Physics2D.BoxCastNonAlloc.html
    "
    Description
    Casts a box into the Scene, returning Colliders that contact with it into the provided results array. Note: This method will be deprecated in a future build and it is recommended to use BoxCast instead.
    "


    From what I have read, heard and understand, the suggested BoxCast (Circle and Capsule included) cause Garbage Collection on use, while their NonAlloc offspring do not. Since I will be using these 'shape' raycasts quite frequently in my game (as in having multiples of them running in FixedUpdate()), why is the documentation encouraging the use of something that will flood a game with GC over an (I assume) far more performant method?

    And for that matter, when is this supposed deprecation scheduled to happen? That disclaimer has been around for years now and NonAlloc still exists and works just fine.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    If you look at the second and third overloads for BoxCast they are non-allocating. That is what has replaced the NonAlloc method.

    https://docs.unity3d.com/ScriptReference/Physics2D.BoxCast.html

    public static int BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance = Mathf.Infinity);


    public static int BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance = Mathf.Infinity);


    The one that is bad and causes GC allocations is this, because it always returns a brand new array:
    https://docs.unity3d.com/ScriptReference/Physics2D.BoxCastAll.html
     
  3. Seanchaoz

    Seanchaoz

    Joined:
    Jun 19, 2019
    Posts:
    18
    Ahhh it all makes sense now, thanks. Exactly the answer I was fishing for :)