In the game I am working on, I am creating a level system to make it easy for other people to create maps for my game. The level system works by having a project that is completely open to the public that contains a prefab named Level and some editor scripts that package it into an AssetBundle. The project also contains a component (let’s call it ComponentA) that is also inside of my game’s project. The idea is that you can put ComponentA on one of your objects inside the Level prefab, and when the game loads, it detects all objects with ComponentA (using GameObject.FindObjectsOfType) gets a value from them, and runs some code on them.
My problem:
When running GameObject.FindObjectsOfType, Unity did not discover any ComponentA components. When I checked the objects in the inspector, they had a ComponentA component, but the image was blank, and when I double clicked on the script, instead of taking me to the ComponentA file, it took me to a page that said Assembly Information. I tried adding an object to the scene (not the AssetBundle) with a ComponentA component, and Unity was able to detect it just fine with GameObject.FindObjectsOfType.
Does anyone know what could be causing this issue, and how to fix it. If I need to switch to Addressables to fix it, that is perfectly fine.
Unfortunately I did not, but I believe it may stem from the components having different GUIDs across the two projects. Try starting there. Also, let me know if you find anything. I dropped that project a while ago, but have since started a new one and am looking to do something similar.
If the project contains the .meta files, the GUIDs should match.Keep in mind that asset bundles do not contain any code: any components and scriptable objects in a bundle contain just serialized data and the code must already exist in the game’s executable.
However, if those components aren’t used directly or indirectly by any scene of your game, their code will not be compiled in into the game’s executable and the components will not work when added to bundles afterwards. The same problem happens with classes that are accessed only via reflection: Unity can’t know they are used and need to be part of the build.
Read about managed code stripping and the various methods to preserve unreferenced types:
Also, when doing such kind of “users can create bundles to load into the game” projects, it’s better to have your code be in managed DLLs instead of loose scripts. This greatly reduces opportunity for user error since they can’t accidentally modify/delete scripts.