- Home /
Asset bundles and scene references
TL;DR It seems that loading a scene doesn't seem to care about AssetBundles, and just creates new instances instead of grabbing them from a loaded bundle. Is this a bug that I should report?
In order to detect the surface type of gameObjects in the scene (wood, stone, dirt etc. mostly used to play the right sound and vfx), I decided to used PhysicsMaterials. This way I can know from any collider what its surface type is. However, I'm having the following issue with AssetBundles and scenes:
I have my PhysicsMaterial in a bundle that we will call "bundleA".
I have some prefabs with some colliders with a reference to these PhysicsMaterials in a bundle that we will call "bundleB".
I have scenes with instances of these prefabs.
When I load bundleA then bundleB, everything is working perfectly, the instance of the sharedMaterial of the gameObject's colliders is the exact same one as the ones in the bundleA.
When I load bundleB then bundleA, it's obviously not working, that's why I am making sure when I load a bundle that I always load all of its dependant bundles first.
However, when I load a scene, even if I load bundleA before opening that scene, all the gameObjects in that scene have a different instance of the PhysicsMaterials, which means they don't have the same HashCode or InstanceID or anything... I really don't want to compare names because of how slow and inconsistent that could be, and I was wondering if there's any way to tell Unity to not create a new instance of these PhysicsMaterials and grab the ones of the loaded bundleA instead (which to me should be the default behaviour, maybe it's just a bug?)
Has this happened to anyone before? Does someone have a solution?
Thanks in advance!
Answer by Alphius · Aug 14, 2018 at 03:29 PM
Figured it out: Unity does not load bundle dependencies when calling SceneManager.LoadScene, so you have to load them yourself first. If you do not do it then Unity will create new instances.
I was actually doing this, except I forgot the first scene, the one that gets loaded when the game starts. Unity does not load its dependencies either, which means you have to load an empty scene first, then load the bundles of your first scene, then load the actual scene. Annoying, but I guess it makes sense. Closing this topic.