Unity Tutorial : How to Seamlessly playing music between multiple scenes
Вставка
- Опубліковано 13 жов 2024
- Simple tutorial on how to seamlessly play music over multiple scenes.
On Scene one the music starts, then when the user goes to scene two the music continues played from the point it was at in scene 1.
If you enjoyed this video please leave a like and subscribe!
Thanks for watching cheers.
▶ Subscribe to channel - bit.ly/3cY5vcT
Song name:
Eels - I Need Some Sleep (Dwin Remix)
• Eels - I Need Some Sle...
Thank you for the tutorial. I had an issue where I needed the music to play between two specific scenes, and then be destroyed upon load of any other scene. I saw that some other people in the comments are having this issue too, so here is the solution I found: If you are using Unity's scene management simply find the audio source object in that script and destroy it before calling the SceneManager.LoadScene() function. This way, your audio source object will be destroyed before loading specific scenes.
Thanks for the tutorial - thought I recognized the accent :) glad to see more South Africans on the platform producing good content
Worked like a charm. Thanks man. And also thanks for responding to me on the last video alerting me to this video.
No problem Bru 🤟 glad it helped you out!
Bro you are so helpful really! You are making videos very detailed and also explaining things that are more specific and that's what I like from your tutorials. Thanks :)
Your tutorials are really helpful they allowed me to finish my game Tysm
Really great tutorial! Could you make a series explaining how to make an inventory of items that boost a character's stats? Along with an explanation of how to set up a the game to where enemies drop stat boosting items and the player can pick them up?
very good and worked but bro if we want move to a third scenes and we don't want to use this music only in first and second scenes , what we do ? and thank's
Great tutorial! I used it to make a soundtrack between scenes, but it also taught me how to not have objects destroy between scenes which was an issue I had in the game I am working on. I have a level lock/unlock system that was badly made and had issues when using between scenes/levels but this seems to have completely fixed it without meaning to. Thanks :D
Thank you for sharing. You helped me figure out how to control an audioSource from another scene.
Thanks for the tutorial, helped a lot, happy holidays!
Took a little tweaking but worked like a charm in the end 👍
Worked just perfectly! Ty Man your amazing
Thanks so much! Really appreciate your tutorial😊 works perfectly
Glad it helped 😁
How to stop the music when on scene number 3?, in my case i have 3 scene
So I have a scene where another music track has to play and the first music track plays through it, and idk how to code so I cant make the second track play alone, what can I do?
So, I used this, and it works when I transition between my start menu and controls screen but when I go to my level select screen it also overwrites the audio that I have set there is there a way for it to only play continuously for a select few screens then have other audio take over when gong to another scene?
error CS1061: 'GameObject[]' does not contain a definition for 'length' and no accessible extension method 'length' accepting a first argument of type 'GameObject[]'
Hello, can you provide an example to adjust the music by clicking the on off button and slider simultaneously?
Only tutorial that works thx
I have an problem
error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.GameObject[]'
I have this problem too. Can we get some help?
Update: I found the answer. Make sure you have an “s” so it’s plural “objects”
GameObject[] musicObj = GameObject.FindGameObjectsWithTag(“GameMusic”);
dini scsene ko tag dena paryga ? ya bs scene 1 ko ?
TY! Exactly what I needed and works instantly! :D Deserved LIKE!
Your channel is a blessing thank you so much
Thanks mate!
And how should I do this if I have 2 audio sources?
You bloody star. Thank you.
what if you want to change music, like when in the boss stage, clearly dontdestroy will keep the pre music, so how to change music sir
Depends on what you want if you just want a different music track to play make the boss fight its own scene then play a different music track.
Hey! But if I want for example to stop it on scene 3 ( I have there another song) What should I do?
i have the same problem
not a expert but maybe what you can do is not drag the music components to that scene?
i'm in this position too...
Did you solve the problem? I have same problem too.
Same problem here
Thanks a lot man, it helped me :)
Hi thanks for this tutorial, how can i put another extra scene and just stops all the music. I have for example 1 menu scene i play the music then i play the level 1 the music stops from menu and starts the level 1, finish the level 1 and wanted to hear the same music in level 2 but when im going back on the main menu i wanted to stop the music from the levels and only plays the main menu music. Thank you. : )
Hey Bud,
Okay so hopefully I can explain this here ( let me know if it solves your problem ) ... So what you need to do is:
1st: on your 'Main Menu' remove the script 'DoNotDestroy' from your audio source. ( this way when you open up the main menu the 'Main Menu Music' Audio will play and when you go to scene 1 you can start a new 'Game Audio Sound' .
2nd: So I'm presuming you have a button to go to the main menu from the scene 1, scene 2, etc... ( like a Menu within your game ) . so on that button you just need to add the line:
Destroy(GameObject.Find("Audio Source"));
This will destroy the 'DoNotDestroyOnLoad' object then I would recommend you immediately go to the main menu scene. so your code should look something like this:
public void GoToMainMenu()
{
Destroy(GameObject.Find("Audio Source"));
SceneManager.LoadScene("scene_1");
}
How this will work is the DoNotDestoryOnLoad object will be destroyed on whatever 'Game' Scene you are on and then when you are on the main menu, the main menu music should start to play!
Hope that helps!
Cato
@@catodevs Thank you for your faster awnser i will try it out. Thanks. 🙌
No problem🤟
how do inly make it play on only two scenes
how to do that if i open my main menu then the don't destroy on load will be disabled?
why my background music did not play when i build to my android device ??
Simple and useful
Thanks man
Thanks for the video. Though the slider for scene_1 moves, the Audio Source slider in the Inspector does not. Any ideas?
As a follow-up, it seems the issue is with the PlayerPrefs reading the volume. I searched the registry and could not find that keyword.
With some more searching, (I called the key 'volumeabc') since 'volume' is used a lot in the registry. Of course the registry holds the Float as a D Word, so it thinks it's invalid, but it seems to work...
Great. But how to change the music? I have 3 scenes and I want one music on the first two and another on the third. But for some reason, the first music remains, and the second is destroyed. Help please
Okay guys, I did it. I had to remove the tag search (This line: Object Music = GameObject.FindWithTag("GameMusic")) and add them separately on the stage with music. But now it's changing the music. (you need to specify new music on the first scene from its beginning, not on every one. Only on the stage where it changes)
Thanks again. Here is the modified code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class DoNotDestroy : MonoBehaviour
{
private void Awake()
{
GameObject[] musicObj = GameObject.FindGameObjectsWithTag("GameMusic");
if (musicObj.Length > 1)
{
Destroy(musicObj[0]);
DontDestroyOnLoad(musicObj[1]);
}
else
DontDestroyOnLoad(this.gameObject);
}
}
UPD: I found an error in the code, it was impossible to change the volume. here's the solution: add these lines to the audio controller script and declare this function in Start, it should help
private void Start()
{
StartCoroutine(UpdateAudio());
}
IEnumerator UpdateAudio()
{
yield return new WaitForSeconds(0.1f);
Object Music = GameObject.FindWithTag("GameMusic");
sMusic = ObjectMusic.GetComponent();
}
I explain. We delay the detection of music, because it is changing with us. The fact is that Unity immediately finds the last sound and adjusts it, and now it waits until they change and only then begins to search.
Good luck
Please Keep making Videos like this!!
In my game I want that in the menu an other music plays than in all the other levels but in the other levels I want the music not to restart every time the next level loads. How can I switch the music when i'm in the menu. Please help
Thank you!
thank you!
My audio is still being played twice but only when i return from scene 2 to my main scene... any fix?
Take a look at the tutorial again it should work :)
What if we have a slider only on the first scene in a popup, and it has to control sound on all scenes in an app?
Howsite,
It's possible. You will just have to create a new script for the 'other' scenes to fetch the player pref value(for the volume) and update the audio source volume without the GUI(slider).
Basically when you start the new scenes ( without the slider) in the code you will have to update the audio source volume once off.
Hope that makes sense.
thank you
it work, thanks
can we just convert the audio source into a prefab?
because changing a prefab's value will persist between scenes.
and then use playerprefs to get the saved value on awake
But when you will go back to scene1 then it will create another audio file which will duplicate the music. Same happened with me. Any solution??
use Singleton pattern for the audio source
REALLY COOL
I have more than 2 scenes and when i switch back to the main scene the music restarts but the previous one continues to play making 2 songs play at once
I'm assuming you got this figured out a long time ago, but here's an answer for posterity:
In your script (in my case, called music), you can check on void Start()
{
if(FindObjectsOfType().Length > 1)
{
Destroy(gameObject);
}
}
This checks if there is already an object with the script (in this case called music) attached, and if there is, it will delete the new one, so there should never be more than one.
Hi so i am wondering i have a main menu at the start of my game i want to play the music in settings and credits but i want the music to stop when i press the play button how can i do that can i create some new script and attach it to the play button or something if so can you provide some code in a stopmusic script?
Howsite bud, yeah I can work on that for a next video 👍
@@catodevs ok thanks
Can't wait for the next video
not works(
My problem is music dont plays continuesly
thanks
I need help it does not show the DonotDestroyOnload so it is not working :( :( :(
how do i stop the music when i enter a new scene?
edit: i fixed it
How?
tnx you so much!
Thank you dude!
Okay. But suppose i have a Scene 3, and i DON'T want the audio playing in that scene. How would i go about that?
Write a new script, find the object (Find with tag, MusicManager is the tag for this example) and :
m_MyAudioSource = GameObject.FindWithTag("MusicManager");
m_MyAudioSource = GetComponent();
m_MyAudioSource.Stop();
You might have to do the stop in Update, and obviously do the initializing/finding in Start/Awake
@@023achilles where would you put this script? in the on click for a start menu for example?
@@omarcano6195 This guy I was replying to wanted it to just stop the music once a new scene was loaded. Assuming because he wants to play new music in the new scene, so he doesn't want both playing at the same time.
If you are already familiar with using buttons (canvas, etc.) then yes, you can use this to stop the AudioSource/music.
Make sure your game object that is playing the music has a tag "MusicManager" (or whatever you want to call it) though. No spelling errors, no spaces allowed!
thanks a lot!
how to remove music in the third scene
Would this work for when if we go back to scene one again right after opening scene 2? I've tried doing stuff by having a bunch of stuff donotdestroyon load but i keep having a reference error, any help on that? I'll try to do what you did in this video and see if that fixes my problem on when i go back and load my first scene again
Edit: turns out yeh I just needed that extra line of finding the tag in the next scene to get it to work
Thank you Thank you Thank you
Thank you a lot
at the 7:21 I don't know how to play the 2 song... i am bad at English, can you also type how to make 2 song?
or i just made the same script but is number2 script?
Thanks!
Thx
a lov u men.
Adamsın adam
doesnt work
🐕
Thank you so much!!! You Saved
Thank you !
thanks
Thank you !