Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by YoYoYoYo_111_PiPi · Oct 08, 2016 at 08:37 AM · audiomultiplescenes

Playing audio through multiple scenes

Hi I found this video on YouTube, about playing music through multiple scenes without the sound breaking:

Playing Seamless Music Across Scenes in Unity 5 With Non-Destroyable Object

Here is the code from the video:

 using UnityEngine;
 using System.Collections;
  
 public class DontDestroy : MonoBehaviour {
  
     void Awake ()
     {
         GameObject[] objs = GameObject.FindGameObjectsWithTag("music");
         if (objs.Length > 1)
             Destroy(this.gameObject);
  
         DontDestroyOnLoad(this.gameObject);
  
     }
 }

You attach it to the GameObject that has an audio, and create a tag for it so you can call it in script.

I was just wondering is it possible to add lines to the same code so that once it enters a certain scene, this GameObject stops appearing in it... the audio doesn't play for that scene...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YoYoYoYo_111_PiPi · Oct 08, 2016 at 10:27 AM 0
Share

I've asked the person on YouTube channel, and was told:

'To stop in a particular scene, I would test for the scene number and then if there's a match call a Stop() on the music, otherwise Play().'

But since I'm a code dummy, I have a problem of even writing that down (i.e. have no clue how it goes)... So, If there's someone to whom this ain't no problem to write, it'd be awesome to update this code (for music stopping at a particular scene).

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by YoYoYoYo_111_PiPi · Oct 08, 2016 at 07:04 PM

Okay, so this is the code I used, and it does what I wanted (Unity 5.4):

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class DontDestroy : MonoBehaviour {
 
     void Awake ()
     {
         GameObject[] objs = GameObject.FindGameObjectsWithTag("music");
         if (objs.Length > 1)
             Destroy(this.gameObject);
 
         DontDestroyOnLoad(this.gameObject);
 
     }
 
     void Update()
     {
         if (SceneManager.GetActiveScene().name == "SceneName")
         {
             Destroy(this.gameObject);
         }
     }
 }

Instead of a 'SceneName', you write your own name of the scene where you want audio to stop. Hope this might help someone out. Peace.

EDIT: If you have multiple scenes to go from these, that keep playing the audio, and you want to stop it in multiple instances, just copy the...

 if (SceneManager.GetActiveScene().name == "SceneName")
         {
             Destroy(this.gameObject);
         }

... and add the other scene name.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image waencumsuraeni · Nov 26, 2019 at 06:11 AM 0
Share

YO, THAN$$anonymous$$ YOU SOO $$anonymous$$UCH FOR $$anonymous$$Y 2 DAY PROBLE$$anonymous$$S. IT WOR$$anonymous$$S VERY WELL. YOU'RE HERO. GOD BLESS YOU.

avatar image kahaw_1998 · May 15, 2021 at 02:18 PM 0
Share

how about when back to the previous scene? Since the dontdestroyonload's object is destroyed, so the music won't play anymore, what if I want to play the music rat the previous scene? (Scene 1 > Scene 2 > Scene 3 ) - Destroy dontdestroyonload's object in scene 3, but want to play the same music when I switch from (Scene 3 > Scene 2/ Scene 1) without stop or restart the music.

avatar image
0

Answer by skupasia · Oct 03, 2017 at 06:54 PM

@YoYoYoYo_111_PiPi I tried using this script, but I get this error:

Assets/Scripts/DontDestroy.cs(21,27): error CS0117: SceneManager' does not contain a definition for GetActiveScene'

Do you know why it might be?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YoYoYoYo_111_PiPi · Oct 05, 2017 at 02:28 PM 0
Share

I am not sure, I haven't been using Unity lately. So i also haven't updated it either... I know that with new updates Unity also changes the code language. Though 'Scene$$anonymous$$anager.GetActiveScene' is still there in the official scripting documents:

https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.GetActiveScene.html

Check if you have used everything correctly as in the code above (stuff like 'using UnityEngine.Scene$$anonymous$$anagement;') and you have to type the name of your scene, whatever it is called, ins$$anonymous$$d of "SceneName", as given in example. If you still have problems with it, please also check out the Unity forums for that error, because I can't say for myself that I'm a coding expert in any way, and I've used that one quite a while ago.

Cheers.

avatar image FullMe7alJacke7 · Oct 05, 2017 at 03:45 PM 0
Share

Do you have " using UnityEngine.Scene$$anonymous$$anagement; " at the top of your script?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Additive Scenes, best way to hide content? 1 Answer

Cross-Scene Simulation? 0 Answers

Character Sound is not sound 0 Answers

Complicated level change problem 1 Answer

How can I merge 2 scenes into one project? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges