Search Unity

Feedback Improve UnityEngine.Object.Destroy

Discussion in 'Editor & General Support' started by Peter77, Jan 8, 2022.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    In every Unity project I come across a custom
    Destroy
    method, that can handle whether the editor is in Edit or PlayMode, such as:
    Code (CSharp):
    1. /// <summary>
    2. /// Destroys a UnityObject safely.
    3. /// </summary>
    4. /// <param name="obj">Object to be destroyed.</param>
    5. public static void Destroy(UnityObject obj)
    6. {
    7.     if (obj != null)
    8.     {
    9. #if UNITY_EDITOR
    10.         if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused)
    11.             UnityObject.Destroy(obj);
    12.         else
    13.             UnityObject.DestroyImmediate(obj);
    14. #else
    15.         UnityObject.Destroy(obj);
    16. #endif
    17.     }
    18. }
    The above code has been copied from the Universal Render Pipeline, see https://github.com/Unity-Technologi...nes.core/Runtime/Utilities/CoreUtils.cs#L1142

    Feedback:
    1. Having a single Destroy method only, which can handle both Edit and PlayMode, would be ideal.
    2. If 1. isn't possible, why don't you provide another Destroy method, let's call it
      UnityEngine.Object.SafeDestroy
      and it's able to handle Editor and PlayMode, so we don't have to implement it for every project?
     
    timmehhhhhhh and adamgolden like this.
  2. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,118
    what do you need the editor part for a runtime application? did you have issues when testing in the editor your game with the UnityObject.Destroy(obj); part?