- Home /
Create customized prefab at runtime and save it
So I'm creating a soldier customization to my game. We got two prefabs. A helmet prefab and soldier prefab. After player selected which helmet he wants the helmet prefab is instantiated into already existing soldier skeleton(so the animation looks okey with the helmet on). Then player mostlikely will close the customization menu and with that soldier connected with helmet. My question is: how can I save soldier and helmet as one prefab on runtime so it would be instantiated later in the game? I tried saving it into List but after deleting soldier (that already has helmet in it) it also disapears from the List. I'm propably missing something very crucial here so I ask you for help. Thanks!
Answer by Magso · Apr 13, 2020 at 01:30 AM
If the helmets are in a list or array, save the index number selected as an integer variable to retrieve later on an object with DontDestroyOnLoad()
or a script with [RuntimeInitializeOnLoadMethod]
.
DontDestroyOnLoad() would work but not in that case cuz after customization player is still on same scene. There is still prefab of customized soldier in the current scene that is SetActive(false) but when player want to customize another soldier my code deletes previous soldier that was SetActive(false). And I still dont know how to save these soldier prefabs somewhere.
You can't create a new prefab as of yet, https://forum.unity.com/threads/unity-create-prefab-at-runtime.479073/
You'll need to store separate values for each player, this could be done with a dictionary
Dictionary <int, int> players = new Dictionary<int, int>();
players.Add(playerID, helmetID);
By player I meant soldier if it more than one soldier per player
I see, damn. Thank you very much. I got to change a lot in my code now xd
Your answer
Follow this Question
Related Questions
Cloned Prefab 3 Answers
Objects flickering between dark and light 7 Answers
Get the name of an instance's prefab at runtime? 0 Answers
Create with code 2.2 pizza not shooting 1 Answer
Add 0.5f to Vector.Right on next instantiated object ? 1 Answer