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

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.

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 Unity - Scripting API: PrefabUtility.IsPartOfPrefabInstance to check if your object if part of a Prefab instance then call Unity - Scripting API: PrefabUtility.UnpackPrefabInstance to unpack the instance. Make sure the unpack mode is Completely, and finally you can destroy your objects.

2 Likes

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…

3745645–311215–project.zip (19.2 KB)

Thanks we will take a look at it

1 Like

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.

2 Likes

This issue seems to be fixed in b9.