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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How would I go about using music from StreamingAssets folder (imported by the player) 0 Answers

getting rough BPM of AudioClip 1 Answer

Audio Manager or not? Audio System 0 Answers

C sharp cant play Audio 2 Answers

GetSpectrumData over entire file 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges