x


Music on multiple scenes

function Awake()

{ var gameMusic : GameObject = GameObject.Find("GameMusic");

DontDestroyOnLoad(gameObject);

if (gameMusic)
    {
        Destroy(gameMusic);
    }

}

This script allows the background music to play through the different scenes, but when I get back to the main menu page, the music starts all over again, overlapping the one which is continuing from before. How do I stop this from happening?

Thank you.

more ▼

asked Sep 06 '10 at 09:29 AM

nicole gravatar image

nicole
21 12 15 22

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

2 answers: sort voted first

try putting your music player on a level before your main menu page.

more ▼

answered Sep 06 '10 at 10:10 AM

AliAzin gravatar image

AliAzin
2.5k 42 56 79

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

Another more robust way would be to make the sound player singleton. Seems like you are doing it backwards and destroys the old object and allows the new one to be created. Some pseudo code:

void Start()
{
   // Find GameObjects of same type
   // if num > 0
   // Destroy this.gameObject
   // Return

   // Do normal stuff
}
more ▼

answered Mar 18 '11 at 04:30 PM

Niklas gravatar image

Niklas
179 7

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

x1029
x764
x525
x263

asked: Sep 06 '10 at 09:29 AM

Seen: 1601 times

Last Updated: Sep 06 '10 at 09:29 AM