Unity Tutorial : How to Seamlessly playing music between multiple scenes

Sdílet
Vložit
  • čas přidán 31. 08. 2020
  • 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...

Komentáře • 108

  • @SteliosKrom
    @SteliosKrom Před 8 měsíci

    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 :)

  • @neonwolf07
    @neonwolf07 Před 2 lety +3

    Your tutorials are really helpful they allowed me to finish my game Tysm

  • @CyberStudios
    @CyberStudios Před 2 lety +3

    Thanks for the tutorial - thought I recognized the accent :) glad to see more South Africans on the platform producing good content

  • @throkgul
    @throkgul Před 3 lety +2

    Thank you for sharing. You helped me figure out how to control an audioSource from another scene.

  • @seanmcculley1160
    @seanmcculley1160 Před 3 lety +8

    Worked like a charm. Thanks man. And also thanks for responding to me on the last video alerting me to this video.

    • @catodevs
      @catodevs  Před 3 lety +1

      No problem Bru 🤟 glad it helped you out!

  • @estevves3903
    @estevves3903 Před rokem

    Thanks for the tutorial, helped a lot, happy holidays!

  • @Jmody13
    @Jmody13 Před rokem +1

    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

  • @mizzle5630
    @mizzle5630 Před 2 lety +1

    Worked just perfectly! Ty Man your amazing

  • @tristanradeka2274
    @tristanradeka2274 Před 4 měsíci

    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.

  • @CC-ki7pv
    @CC-ki7pv Před 3 lety +1

    Simple and useful
    Thanks man

  • @khadijahfairuz2955
    @khadijahfairuz2955 Před 3 lety +3

    Thanks so much! Really appreciate your tutorial😊 works perfectly

  • @oivalaine9045
    @oivalaine9045 Před 2 lety +2

    Took a little tweaking but worked like a charm in the end 👍

  • @CryT4x
    @CryT4x Před rokem

    TY! Exactly what I needed and works instantly! :D Deserved LIKE!

  • @Richartichaut
    @Richartichaut Před 2 lety +1

    Thanks a lot man, it helped me :)

  • @stealurwife2489
    @stealurwife2489 Před rokem

    Thank you so much!!! You Saved

  • @louaybenhaloua
    @louaybenhaloua Před rokem +6

    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

  • @egoriches1043
    @egoriches1043 Před 3 lety +1

    Thank you !

  • @MT-on4hn
    @MT-on4hn Před rokem

    Your channel is a blessing thank you so much

  • @mitch516
    @mitch516 Před 3 lety

    Thank you dude!

  • @CatalystMaker-y7c
    @CatalystMaker-y7c Před 11 měsíci

    Only tutorial that works thx

  • @flym0
    @flym0 Před 9 měsíci

    You bloody star. Thank you.

  • @clasherkingytofficial2936

    Please Keep making Videos like this!!

  • @MaestroMarc
    @MaestroMarc Před 2 měsíci

    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?

  • @denn501
    @denn501 Před 2 lety +1

    thank you!

  • @TrippyGamer
    @TrippyGamer Před 3 lety

    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

  • @omarcano6195
    @omarcano6195 Před rokem +1

    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?

  • @GamepadStudios
    @GamepadStudios Před rokem +1

    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?

  • @katashisano
    @katashisano Před 3 lety +1

    REALLY COOL

  • @youtuberfail3234
    @youtuberfail3234 Před 3 lety +2

    How to stop the music when on scene number 3?, in my case i have 3 scene

  • @HadiNauf
    @HadiNauf Před 2 lety +1

    it work, thanks

  • @aliguliyev2122
    @aliguliyev2122 Před 3 lety +1

    thanks a lot!

  • @BEN-cj6se
    @BEN-cj6se Před rokem

    tnx you so much!

  • @naughtycat-3362
    @naughtycat-3362 Před 2 lety

    Thank you a lot

  • @iiznub1232
    @iiznub1232 Před 2 lety

    Thanks!

  • @suleiman8610
    @suleiman8610 Před 2 lety

    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!

  • @caaacaa24
    @caaacaa24 Před 8 měsíci

    Hello, can you provide an example to adjust the music by clicking the on off button and slider simultaneously?

  • @Syvre_
    @Syvre_ Před 2 lety +1

    thank you

  • @luckydenzel
    @luckydenzel Před rokem

    how to do that if i open my main menu then the don't destroy on load will be disabled?

  • @st4xcs
    @st4xcs Před rokem

    error CS1061: 'GameObject[]' does not contain a definition for 'length' and no accessible extension method 'length' accepting a first argument of type 'GameObject[]'

  • @underguysports_1359
    @underguysports_1359 Před 3 lety +1

    Thank you Thank you Thank you

  • @toushalsewruttun443
    @toushalsewruttun443 Před 3 lety

    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

    • @toushalsewruttun443
      @toushalsewruttun443 Před 3 lety +3

      Edit: turns out yeh I just needed that extra line of finding the tag in the next scene to get it to work

  • @MrSsayedd
    @MrSsayedd Před 2 lety +1

    thanks

  • @jhonjosephpagaduan826
    @jhonjosephpagaduan826 Před 2 lety

    why my background music did not play when i build to my android device ??

  • @kidcode9565
    @kidcode9565 Před 3 lety +1

    Thx

  • @majabjeletic
    @majabjeletic Před 3 lety +1

    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?

    • @catodevs
      @catodevs  Před 3 lety +1

      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.

  • @checkerface5710
    @checkerface5710 Před 3 lety

    I need help it does not show the DonotDestroyOnload so it is not working :( :( :(

  • @cjd0510
    @cjd0510 Před rokem

    how do inly make it play on only two scenes

  • @muhammadsaib-sm6sg
    @muhammadsaib-sm6sg Před 4 měsíci

    dini scsene ko tag dena paryga ? ya bs scene 1 ko ?

  • @kaszana9698
    @kaszana9698 Před 2 lety

    how to remove music in the third scene

  • @mcleverm01
    @mcleverm01 Před 2 lety +1

    Thanks for the video. Though the slider for scene_1 moves, the Audio Source slider in the Inspector does not. Any ideas?

    • @mcleverm01
      @mcleverm01 Před 2 lety

      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.

    • @mcleverm01
      @mcleverm01 Před 2 lety

      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...

  • @viarori
    @viarori Před rokem +1

    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

    • @omarcano6195
      @omarcano6195 Před rokem

      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.

  • @minhalraza9459
    @minhalraza9459 Před 2 lety

    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??

  • @coolio3856
    @coolio3856 Před rokem

    My audio is still being played twice but only when i return from scene 2 to my main scene... any fix?

    • @catodevs
      @catodevs  Před rokem

      Take a look at the tutorial again it should work :)

  • @TomTheHuman
    @TomTheHuman Před 2 lety

    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

    • @023achilles
      @023achilles Před rokem

      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.

  • @AhmetKocyigitt
    @AhmetKocyigitt Před 3 lety

    Adamsın adam

  • @bottleogames
    @bottleogames Před 3 lety +1

    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?

  • @Simulatech
    @Simulatech Před rokem +1

    how do i stop the music when i enter a new scene?
    edit: i fixed it

  • @teslormangaming4536
    @teslormangaming4536 Před 2 lety +1

    not works(
    My problem is music dont plays continuesly

  • @talekey7497
    @talekey7497 Před 2 lety

    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?

    • @talekey7497
      @talekey7497 Před 2 lety

      or i just made the same script but is number2 script?

  • @uiriamusukai2794
    @uiriamusukai2794 Před 3 lety +3

    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. : )

    • @catodevs
      @catodevs  Před 3 lety +19

      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

    • @uiriamusukai2794
      @uiriamusukai2794 Před 3 lety

      @@catodevs Thank you for your faster awnser i will try it out. Thanks. 🙌

    • @catodevs
      @catodevs  Před 3 lety +3

      No problem🤟

  • @stanislavkosytskyy6637
    @stanislavkosytskyy6637 Před 3 lety +9

    Hey! But if I want for example to stop it on scene 3 ( I have there another song) What should I do?

  • @Carricx
    @Carricx Před rokem

    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?

    • @023achilles
      @023achilles Před rokem

      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

    • @omarcano6195
      @omarcano6195 Před rokem

      @@023achilles where would you put this script? in the on click for a start menu for example?

    • @023achilles
      @023achilles Před rokem

      @@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!

  • @theunboxerhemerinthewoods5702

    I have an problem
    error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.GameObject[]'

    • @NewEarthIntegration
      @NewEarthIntegration Před 2 lety

      I have this problem too. Can we get some help?

    • @NewEarthIntegration
      @NewEarthIntegration Před 2 lety

      Update: I found the answer. Make sure you have an “s” so it’s plural “objects”

    • @NewEarthIntegration
      @NewEarthIntegration Před 2 lety

      GameObject[] musicObj = GameObject.FindGameObjectsWithTag(“GameMusic”);

  • @PK-kt9ss
    @PK-kt9ss Před 2 lety +1

    a lov u men.

  • @tunforever6347
    @tunforever6347 Před 9 měsíci

    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

    • @tunforever6347
      @tunforever6347 Před 9 měsíci

      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);
      }
      }

    • @tunforever6347
      @tunforever6347 Před 9 měsíci +1

      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

  • @LadeShootH
    @LadeShootH Před 3 lety

  • @lucutes2936
    @lucutes2936 Před rokem

    doesnt work

  • @robloxiansexclusive1781
    @robloxiansexclusive1781 Před 2 lety +1

    🐕

  • @Polynomial50
    @Polynomial50 Před 3 lety

    Thank you !

  • @Collew
    @Collew Před rokem

    thanks