Search Unity

(Case 1087228) Cannot destroy GameObject that is part of a prefab instance

Discussion in 'Prefabs' started by Peter77, Oct 3, 2018.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    We use the [PostProcessScene] attribute to optimize scenes during build. One of those optimizations is to remove objects and components from Prefab Instances in the scene, that are not required at run-time, with a particular configuration of that prefab instance.

    The game contains simple scene processors, that just remove an object depending on its tag. But we have also more complicated processors that analyze objects, merge those, create new ones, and remove parts of them.

    Often the idea is that a prefab carries more information than needed and we use a scene processor to optimize or cleanup the unneeded parts during a build. Many of those prefab instances are instantiated, placed and configured in the scene via scripting.

    This was working flawlessly from Unity 5 to 2017. Now with 2018.3.0b3, Unity starts to output the following error:
    The provided project is a minimalist reproduce to illustrate the issue. It uses the "EditorOnly" tag to detect which objects to remove for simplicity. I'm aware that Unity removes "EditorOnly" objects itself, but for the sake of simplicity I choose this tag to create a reproduce for you.

    This is actually a big deal for us, because it causes our optimization pipeline to not work anymore.

    Reproduce
    • Open "unity_2017_4.zip" project in Unity 2017.4
    • Create a Windows 64bit Standalone build
    • Notice Unity does not output an error
    • Open project in Unity 2018.3.0b3
    • Create a Windows 64bit Standalone build

    Actual result
    Unity 2018.3.0b3 outputs "Cannot destroy GameObject that is part of a prefab instance." error during the build.

    Expected result
    No error. The functionality from Unity 2017.4, where objects can be removed from prefab instances, should be kept intact.
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Hi,

    We don't want to revert the fact that you can't delete Objects from an instance.

    In previous versions of Unity this would leave the Prefab instance in a disconnected state. When you have nesting this is really bad as it then becomes unclear what it means to apply and revert.

    That said, our internal Scene Post Processors should unpack all prefabs when loading a scene in preparation for building a player it seems that either it does not or your post processor runs before ours.

    When building a scene for the player we strip out all Prefab information and leave all the GameObjects from a Prefab as regular GameObjects. So the fix for you would be to unpack the Prefabs yourself before destroying objects.

    First call https://docs.unity3d.com/2018.3/Doc...nce/PrefabUtility.IsPartOfPrefabInstance.html to check if your object if part of a Prefab instance then call https://docs.unity3d.com/2018.3/Doc...rence/PrefabUtility.UnpackPrefabInstance.html to unpack the instance. Make sure the unpack mode is
    Completely
    , and finally you can destroy your objects.
     
    phobos2077 and Peter77 like this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Hi,

    thank you very much for the detailed reply. I implemented unpacking prefabs before doing any scene modifications, but the error didn't vanish.

    After further investigation, I found it's (also) related to using the "EditorOnly" tag. I was able to reproduce this in a new project, with only one prefab where a child of this prefab uses the "EditorOnly" tag. When building a Player, Unity outputs the error.

    Example project attached. I'm also going to update the bug-report...
     

    Attached Files:

  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Thanks we will take a look at it
     
    Peter77 likes this.
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    We found that our build pipeline automatically tries to delete objects with EditorOnly, but did this before unpacking all prefabs. Fix will land in a future beta.
     
    Peter77 and phobos2077 like this.
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    This issue seems to be fixed in b9.