Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Legrandes · Feb 24, 2015 at 04:20 PM · audioaudiosourcemusicplaying

How to make music play always without duplicates?

I have some problem. I wanted to make my audio clip play always between the scenes. So i created a Gameobject, atached a audio sorce, and script public bool Always; public GameObject music;

 void Awake () 
 {
     if (Always)
         {
 DontDestroyOnLoad(music.transform.gameObject);
                 }
         
 }

It works good, but.... The game starts from first scene, then if i move to another scene music is playing. But when i go back to first scene , my gameobject is still not destroyed and music is playing.But it appears a new the same gameobject with this audiosource and there are two audioclips playing at the same moment. Every time when i am going back to first scene new gameobject with audiosource appears and starts playing.

How can i avoid this? Thanks

Comment
Add comment · Show 5
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 meat5000 ♦ · Feb 24, 2015 at 04:20 PM 0
Share

Put a check on the creation of your first music object that one doesn't already exist or the music isnt playing.

avatar image Legrandes · Feb 24, 2015 at 04:39 PM 0
Share

I would like to know how sir )

avatar image meat5000 ♦ · Feb 24, 2015 at 04:41 PM 0
Share

The key is in the object itself that contains the music.

Find the place that the object is created or the music is setup for the first time. Place an if check on it.

Alternatively, gameobject find all the music objects and if more than one if found, delete the duplicates.

avatar image Mmmpies · Feb 24, 2015 at 04:53 PM 2
Share

Also look on the learn site for data persistance. It covers this exact check in that video. Around 15 $$anonymous$$utes in.

avatar image meat5000 ♦ · Feb 24, 2015 at 04:55 PM 0
Share

Nice hint ;)

2 Replies

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

Answer by jarado9 · Feb 24, 2015 at 07:20 PM

 public class MusicPlay : MonoBehaviour
 {
  public static MusicPlay musicplay;
  public bool Always;
  public Gameobject music;
 
 void Awake()
 if (musicplay == null)
 {
 DontDestroyOnLoad(gameObject);
 musicplay = this;
 }
 else if (musicplay != this)
 {
 Destroy(gameObject);
 }
 }
 }


sorry I can only answer once, so had to delete my old answer, and I lost the code you last submitted. Still need know why you need it to be a bool and where you set the true/false value of the bool. else it's hard to help/ understand

Comment
Add comment · Show 5 · 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 jarado9 · Feb 24, 2015 at 08:32 PM 0
Share

The answer I just gave is all I can offer, and I suggest if this doesn't work, to post the code you posted before that was in the answer I deleted, and some detail of where the Always bool is being set to true or false. I will leave it for someone else to answer now, to avoid confusing of deleting my answer again. Sorry if I haven't helped.

avatar image Legrandes · Feb 25, 2015 at 05:57 AM 0
Share

I will try your last code and will see. i just didn't understand when you told to make script static.

avatar image jarado9 · Feb 25, 2015 at 10:51 AM 0
Share

I didn't understand why you have this Always bool because nowhere can see when you give it a true/false value, all I can think is you just tick it as true in the inspector. Hope you sorted it out now, and making it public static is so other scripts can access it from scene to scene, hence and can delete duplicates of itself. sorry for the confusion it was difficult to explain public static until you posted top part of your code as well, so I could see the script name and your declared variables. If my answer has sorted your problem, please accept answer, else I suggest re-paste your code here for someone to answer. or watch http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading which is pretty much where my answer is from, and this training video is very good for explaining keeping data & gameobject from scene to scene, and deleting duplicates.

avatar image Legrandes · Feb 25, 2015 at 03:40 PM 1
Share

It worked. Thanks.

avatar image jarado9 · Feb 25, 2015 at 03:47 PM 0
Share

Your Welcome :)

avatar image
0

Answer by siaran · Feb 25, 2015 at 12:07 PM

I think the best way here is to make your music container a singleton.

See here for an explanation on singleton patterns in Unity: http://unitypatterns.com/singletons/

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Specifying OnAudioFilterRead()'s audiosource 1 Answer

GetSpectrumData over entire file 1 Answer

Glitched Music 0 Answers

PlayScheduled or PlayDelayed 0 Answers

Stereo Mix as Input? 1 Answer


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