- Home /
How to save gameobject created at runtime ?
Hello !
In my game, I ask the user to create a gameobject (from a prefab) which contain an audio source. Then, the user speak to the microphone when pressing start button then it saves the input to the audiosource of the gameobject once the user press stop. The user can put the object in any place of the scene. He can repeat the process in an unlimited way.
However, when the user stop the game and restart it, I want all the gameobjects to be in the same place with the audiosource saved previously. I thought about serialization and putting them to a json file, however it will not take into acount the audiosource... is there a way to really save the gameobject in the scene with the audiosource containing the saved voice ?
I'm trying to find a trick to overcome this issue, but I don't find one. Thank you in advance for your help.
I think you need to have a decode plugin like mp3 for sound. So you can save file.mp3 to actual disk space. Then just json that sound location and your object.
Some Ex: https://forum.unity.com/threads/download-and-save-www-mp3-file.409470/
Answer by Harinezumi · Jun 28, 2018 at 12:11 PM
What you need is to serialize the AudioClip
from the AudioSource
. You can get the data from an AudioClip
using its GetData()
function, and then use the script from this answer or from this forum thread to save it into a wav file to a location you can read from (probably Streaming Assets folder). Then you just have to store what AudioClip
a game object should load.
Exactly this, although I would recommend using Application.PersistentDataPath ins$$anonymous$$d of Strea$$anonymous$$g assets.
~Cheers, LegendBacon
Your answer
![](https://koobas.hobune.stream/wayback/20220612165635im_/https://answers.unity.com/themes/thub/images/avi.jpg)