x


How to play same game music through different levels.

Hi guys,

I have been stuck on reaching an effective method for creating an instance of game music in the main menu and in the levels. What I hope to achieve is one piece of music playing through the different menu scenes and then another piece of music for the game.

The problem I have is when using 'DontDestroyOnLoad' I get music spawning twice!

Any help on how to make a music manager script would be very much appreciated thank you.

more ▼

asked May 03 '12 at 11:53 AM

buxton4life gravatar image

buxton4life
105 6 17 19

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The problem is that the music playing object exists in the scene to begin with, so when the scene is loaded again, a new instance is loaded as well. Since you can't undo DontDestroyOnLoad() that means you have to either load it after the scene is loaded, provided there is no such object in existence already, or destroy the excess one after the scene was loaded.

I personally use this script here to manage all those objects, and tag them "DontDestroy". This script also manages applying the DontDestroyOnLoad() attribute to these objects so I don't have to put it in each individual script I want to keep around.

using System.Collections.Generic;
using UnityEngine;

public class HouseKeeping : MonoBehaviour
{
    private void Awake()
    {
       //make sure the game keeps running even if the application is out of focus so communication to the server/client continues
       Application.runInBackground = true;

       CleanUtilities();
    }

    /// <summary>
    /// Make sure we have only one instance of each utility
    /// </summary>
    private void CleanUtilities()
    {
       //Get all the utilities in current scene
       var utilities = GameObject.FindGameObjectsWithTag(transform.tag);

       //Will contain unique names of utilities
       var uniqueUtilities = new LinkedList<string>();

       if (utilities == null) return;

       for (int i = (utilities.Length - 1); i >= 0; i--)
       {
         //Search if we already have this kind of utility and destroy it
         if (uniqueUtilities.Contains(utilities[i].name))
          Destroy(utilities[i]);

         //If not add it to our unique list and apply DontDestroyOnLoad to it
         else
         {
          uniqueUtilities.AddLast(utilities[i].name);
          DontDestroyOnLoad(utilities[i]);
         }
       }
    }
}
more ▼

answered May 03 '12 at 12:45 PM

asafsitner gravatar image

asafsitner
2.4k 2 8 19

This is perfect! Thank you so much you have saved me so much hassle!

May 03 '12 at 01:03 PM buxton4life

Asafsitner, the script works fine with not destroying or duplicating the music. But how would I switch the music from the menu music to the game music? Sorry for the complication.

May 03 '12 at 02:53 PM buxton4life

Are you using the same object to play both music tracks?

May 03 '12 at 09:40 PM asafsitner

Yes I am..is that wrong :/

I simply have a gameObject named "gameMusic" in both MainMenu and Level1. Both have the same script HouseKeeping and both tagged DontDestroy, the only difference is the music track applied in the inspector. Hope you can visualise that :)

Any help would be awesome. Thanks again.

May 03 '12 at 09:52 PM buxton4life
(comments are locked)
10|3000 characters needed characters left

You can use the singleton pattern. There is a lot of forum posts and questions in Unity Answers regarding this topic.

For example: http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html

more ▼

answered May 03 '12 at 12:49 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

Thanks Nicolas for the reply I will certainly look into it.

May 03 '12 at 01:04 PM buxton4life
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1026
x202
x110
x82
x29

asked: May 03 '12 at 11:53 AM

Seen: 702 times

Last Updated: May 03 '12 at 09:52 PM