Search Unity

Bug Crash when Undoing after calling Transform.SetParent(null)

Discussion in 'Scripting' started by Darkgaze, Apr 22, 2022.

  1. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    This error happens again in all 2021 versions I have. (0, 2, and 3).

    https://issuetracker.unity3d.com/issues/crash-in-transform-setparent-when-trying-to-make-an-undo-act

    As explained in the documentation, SetParent(null) makes World (root of the scene) the parent.
    But when you undo it, it crashes unity.


    I already sent a bug report with a minimal reproducible example.
    Now I have to figure out how to set world as the parent via another way...

    [Edit]

    I researched more, and It happens specifically when you perform a copy of a prefab via
    Unsupported.DuplicatePrefab....()

    It does something wrong with the Undo stack, and if the next operation is modifying the parent (via .parent = null or .SetParent(null)) it crashes Unity.

    A solution would be to call
    Undo.SetTransformParent (myGameObject.transform, newTransformParent, "Set new parent");

    But in the case of duplicating the prefab, it will not create a new entry in the Undo stack, but record that operation and merge it with the previous ones.

    It will not ignore the creation of the object, but in my case this does the trick. ;-)
     
    Last edited: Apr 22, 2022