x


audio.isPlaying always returns false

I am trying to set music to play throughout the game and continue between scenes with an empty gameobject that has the script below attached on my main scene. It starts fine and when I move to other scenes it continues to play, but when I switch scenes back from another scene back to the main scene it plays a second instance. I am checking for isPlaying, but it always returns false. Is this because the gameobject is a new instance so I am not checking the original instance that is first created when the game starts?

function Awake () {

    //Also checked for gameMusic as a boolean, but that didn't work either
    //var gameMusic : GameObject = GameObject.Find("gameMusic");
    //if(gameMusic){ audio.Play(); }

    Debug.Log("is audio playing - " + audio.isPlaying);

    if (!audio.isPlaying) {
        Debug.Log("audio was not playing so it was started");
        audio.Play();
    }

    DontDestroyOnLoad (transform.gameObject);
}
more ▼

asked May 03 '11 at 04:46 PM

cozmoziz gravatar image

cozmoziz
2 2 2 4

Sorry, I used to check for gameMusic as a boolean. Just forgot to take that out.

So original code was

if (gameMusic) { audio.Play(); }

May 03 '11 at 05:29 PM cozmoziz
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

First of all, you search for a game object named 'gameMusic', but then you never use that reference.

Also, if you reload the scene that the 'game music' object is part of, a new instance of that object will be created. If you want only one 'game music' object, it should be part of a scene that's only loaded once (e.g. an 'intro' scene or something of that sort).

more ▼

answered May 03 '11 at 05:21 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

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

I found the answer with the help of using singleton's from this post. I've posted my answer here.

http://answers.unity3d.com/questions/24519/using-singleton-for-background-music

more ▼

answered May 03 '11 at 06:29 PM

cozmoziz gravatar image

cozmoziz
2 2 2 4

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

x1027
x717
x156
x11

asked: May 03 '11 at 04:46 PM

Seen: 1122 times

Last Updated: May 03 '11 at 05:31 PM