- Home /
How can I save multiple instances of a single prefab?
A couple of months ago I started working on a city builder game, all went well till the moment I decided to add a save/load function to the game. The big problem is that the buildings that the player places are prefabs. I tried to do the following with multiple save/load assets I downloaded( basically they handled sterilization and saving/loading from/to a file):
1-Initialize save slots with a name and type.
2-OnButtonClickLoad, A building will send its x,y,z coordinates to a central script which will save it as 3 ints (can't save vector3s for some reason).
3-OnButtonClickSave, the x,y.z int values are loaded and assigned to the buildings vector3.
Only problem is that this only works for a single instance of a prefab (like 1 player camera vector3) but I found it impossible to make it work with buildings that could be of any number in the scene.
Naturally, I tried assigning random generated ids to every building instance, but there was no way I could initialize save slots for an unknown number of instances with unknown ids.
I am quite lost on what to do now as I have pretty much exhausted all of my ideas! How can I save multiple instances of a single prefab?
hi; that seems a big project; i cant help u but give u some idea; but i can work with u on it to see what can we do for it ;
if u want u can install $$anonymous$$m viewer and send me a massage to my gmail : savajjad@gmail.com
good luck
Answer by Faen_run · Oct 02, 2017 at 04:20 PM
If i understand it well you have a scenery with multiple buildings that are prefab clones. If you dont make any changes to them and they are only a prefab in x,y,z coordinates you have to save the coordinate and asign every type of building an ID to link them with it's prefab. Then you can make an object that links every ID with its prefab (a simple list of objects would do, just make a public list of GameObjects and add your prefabs to it, the ID would be the position in the list), so when you load your game your loader read the ID and the coordinates, searches the list and Instantiate that prefab in the position you want.