Загрузка...

How to Play Music Between Scenes in Unity

You want to make a game, but whenever you load a new scene, the background music reverts to the beginning? Don't worry! This tutorial will explain how to fix this problem in the right way. When you import your own audio asset (anywhere on your computer), you will always hear it if you add an audio source object in Unity. But when you navigate to a new scene, the music playback will stop if the scene doesn't have an audio source or it will play the audio from the beginning if there is an audio source already available. What should you do in order to fix it?

First of all, you need to assign a scene loading script that will be used for both buttons in your project. The buttons will be powered with the so-called OnClick() functions, but the scenes need to be present in the Scene Manager, located in the Build Settings.

For moving to another scene, make a simple script that will be part of the audio object. That script, on the other hand, will contain the information which is needed in order to play music between scenes. The method DontDestroyOnLoad is used every time you have an object that should remain in both scenes.

There is also an optional part that includes adding a toggle for controlling if the sound is on or off. The part isn't very well explained, it is just used for testing purposes.

Support the Creator of Kozmobot on the social networks!

CHECK OUT MY WEBSITE: http://kozmobot.com

INSTAGRAM: https://www.instagram.com/kozmobot.channel

TIKTOK: https://www.tiktok.com/@kozmobot.channel

MUSIC IN THE VIDEO: https://www.youtube.com/watch?v=G87ovAyzLEs

Видео How to Play Music Between Scenes in Unity канала Kozmobot Games
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять