r/Unity3D•
SwillyDo

It seems really bizarre that deleting a prefab breaks all objects of its kind in other scenes

Question

I just discovered this 'feature' on something minor (thankfully), and it seems like such a weird design decision I am having trouble believing I understand it correctly.

  1. I make ObjectA in the hierarchy.
  2. I make PrefabA based on ObjectA.
  3. If I delete PrefabA ObjectA still exists in the hierarchy, and I can make a new prefab from it.

That's how it works with one scene. The following is also true:

  1. I make ObjectA in the hierarchy.
  2. I make PrefabA based on ObjectA.
  3. I load a different scene and delete PrefabA.
  4. I return to the first scene and ObjectA is now an empty gameobject complaining about a missing prefab. The data unique to ObjectA is now gone forever.

Where is the logic in that? Why on earth doesn't ObjectA just become a normal object with no prefab reference again? I am very glad I discovered this while messing around rather than losing something important and having to roll back my project.

Posted Sep 10, 2019, 8:25 PM UTC · edited

17
BCSChester•

The reason is because of how prefab data works. When you have a prefab in sceneA and in sceneB, its only one object between both scenes (as far as data storage goes) and unity builds a meta file for that prefab in both scenes that holds the unique data for each instance of the prefab. Otherwise the data for the object would just be stored with the scene and you would have much larger scenes.

There's also quite a few times in production game development that you really want what happened to you, to happen. Prototypical prefabs that are no longer needed can just be deleted from disk and you don't need to worry about some object in another scene with prototype code on it, or missing scripts causing problems

1
SwillyDoOP•

There is no reason not to give the user the option when deleting a prefab to have all instances in a hierarchy to remain as their own objects. Hell, a warning would nice.

It's also weirdly inconsistent that an open scene retains instances of the prefab after it has been deleted but all other scenes get theirs wiped.

0
BCSChester•

By deleting the prefab, you're deleting the data on disk, when you then load that other scene from disk, it can't find the prefab to know what asset to load, but it remains in the active scene, since that scene is in memory. There's no reason, really, to delete a prefab and have the instances remain intact, that's the point of prefab variants as well as a scene having it's own instance of a prefab. Since prefabs brought into a scene can have their own unique parameters separate from the root asset. When you delete a prefab from disk, you're essentially telling unity you don't want that asset to be anywhere and you want the meta data for the prefab wiped, which helps to maintain all the prefabs references and such

1
SwillyDoOP•

you're essentially telling unity you don't want that asset to be anywhere

This is not obvious or necessary. If I delete the archetype it does not necessarily mean all copies of the archetype should be wiped also. It also leaves, permanently, any copies in the hierarchy of whichever scene happens to be open, which is quite arbitrary.

0
BCSChester•

It's because of how a prefab asset works, vs a non prefab asset works, in terms of data serialization.

In short, a non prefab asset in a scene is the data itself, and a prefab is merely a reference to the asset. This is so a prefab that say is 10mb and is in every scene with say 100 scenes won't add 1000mb of data to the build, but instead only 10mb to the build (plus a few mbs for things like unique positions and script values)

When you have a non prefab gameobject in a scene, its data is unique to that scene. The mesh, transform data, or whatever other component data is actually saved with the scene file itself. When you have a prefab in a scene, unity in the backend doesn't see it as a unique object. If you use text serialization and open a scene file in something like vscode, you can see this in action. So a non prefab is in a scene, unity sees all the unique data and says to load the unique data. When a prefab is in a scene, unity will say "load asset with GUID: xxxxxxxxxxxx" and apply values to said GUID reference as per scene instance. When you delete the prefab, you also delete the guid. So since all the actual data for the prefab is on the asset itself and as such tied to the prefab itself, when you delete the prefab it also deletes the meta file that stores said GUID. So then when you load a new scene, unity then runs into an issue where it will no longer have asset with GUID: xxxxxxx and runs into an error. What you can do to mitigate this, however, is before you delete a prefab, if you have a scene that you still want said asset to be in, load that scene, right click the asset in the hierarchy, and select "Unpack Prefab Completely" this will allow unity to then create an instanced object in that scene instead if an instanced reference.

2
Shadow_Being•

Sounds like you should be using prefab variants.

1
bilbaen0•

You are seeing the first behavior because you have the scene with the prefab already open. The second behavior is what happens when Unity tries to load a scene with broken prefabs.

Unity loads the scene's prefabs based on the information from the prefab plus any changes you've made to that prefab in the scene. It does not store each instance of your prefab as it's own object.

When you load a scene and Unity goes to load an instance of a prefab, if that prefab no longer exists all that's left is the changes to the prefab that are local to the scene. Which is why you are seeing the broken objects in scenes you load after deleting the prefab.

​

You keep saying that this is illogical but I don't think you understand the implications. Here is what you are suggesting:

When you delete a prefab, Unity converts all instances of the prefab to GameObjects with the same properties.

​

This has several major drawbacks. Unity would need to do one of the following to make that possible:

  1. When you delete a prefab, Unity opens every scene and loads every single object to make sure the prefab you're deleting is converted to a game object. Then saves those scenes and closes them.
  2. Unity stores all information about each prefab in the scene itself.

Number 1 would take way too much processing, especially as a game grows.

Number 2 eliminates a lot of the benefits of prefabs.

0