Hey guys, I'm making a very basic 2D game and the entire game has only 2 scenes. However, I want to add background music without breaking connection each time I want to swap between the scenes. I have to say that i'm an artist and my knowledge in c# still really basic, any help from the experts? :smile:
Yeah .Wiki said it best Usually i'll create a simple DoNotDestroy manager that will instantiate all the GO's i want preserved between scenes at runtime Awake(). Basically just do a quick search to see if the audio player exists already, if not, instantiate. Doing it this way should ensure you only have a single instance
You can check if the component of a gameobject already exists. So you only start the music when the gameobjects component doesn´t exist. it would be something like (if audio.getcomponent<audio>() == false) {play audio...} if its true it would do nothing
Hi Mike, I did exactly the same, but whenever I return to the main menu the audio file duplicates and it overlaps, you can hear it quite clear in the background, any idea whats happening?
I guess this music go is inside your menu scene and when loading it again you have now two of them.. If that's not what happening you'll have to show us some code
You'l want to make sure the audio source is on a game object that contains a script using this method https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html you might still have hiccups during the load, but that should preserve the audio source between scenes