I've searched on this question and found a lot of info related, but some inconsistencies, and no clear answer.
In a Unity project, I'm creating asset bundles with gameobjects that have scripts (C#). I've learned how to do that, and it works.
My problem is, I ultimately need to preserve the values of the public variables in the scripts that were assigned before building the asset bundle. That is, my prefab has a script with public variables. I instance it many times with different public values for each instance. I need all that to survive the trip through the asset bundle.
My current process is thus:
- Remove scripts from their game objects.
- Build the scripts into assemblies
- Pack game objects and assemblies into asset bundles
- Download the bundles to new project
- Unpack the asset bundles and Instantiate the game objects
- Attach script assemblies to game objects with
AddComponent()
The issue is step 1. I remove the scripts from the game objects before bundling because if I don't, it complains when unpacking that the referenced script is missing. And also, since step 6 uses AddComponent
to attach it, I cant really do that if that script is already an attached component. So removing them before bundling solves those problems.
What am I missing? Where is my method at fault? I read that the public values are saved in the script reference on the game object when bundled, but of course I lose that when I remove the script components.
So, please, how do I maintain public values thru the asset bundling process? Should I NOT be removing script components before bundling? If so, how should I be managing the scripts differently? Would I need to bundle both the script assemblies AND source files? (I currently only bundle the assemblies), and wouldn't that create a redundancy when attaching the assembly?