Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best way to handle background music

Discussion in 'iOS and tvOS' started by smith88, Jan 20, 2009.

  1. smith88

    smith88

    Joined:
    Nov 9, 2008
    Posts:
    9
    I have a number of different scenes in my game and some of which have the same background music. So, moving from scene to scene, in most cases, involves the continuation of the music that is currently playing - not sure if this is clear? Anyway, I set up a gameobject on which I call DontDestroyOnLoad. It is responsible for playing the music for any given scene. Then I have the scene, as it starts up, find the tagged sound object and tell it what music it needs to play. This does work to some degree, but I have a couple of problems:

    1) the music stutters on transition from one scene to another
    2) after moving through a bunch of scenes, the music stops playing. I can't seem to figure out why. If the scene couldn't find the sound object, to tell it what to play, it should keep playing what it is currently playing.

    Any thoughts? Is there a better approach? Anyone else run into this problem? I'm done my game (first game), but I would like to solve this before submitting.
     
  2. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    You could also have your "main" scene with all the initialization and also your music manager object and load every real game scene then by LevelLoadAdditive.
     
  3. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    With the LoadLevelAdditive method, would it be possible to close previous levels without dumping everything? Say you load the base persistent level with the bg music and then additively load another level (we'll call it LevelA) on top. How would you close LevelA and open LevelB without closing the persist? Unless I'm mistaken, LoadLevel(LevelB) would close all open levels and open B only. LoadLevelAdditive(LevelB) would open B on top of A and persisit without closing either.
     
  4. smith88

    smith88

    Joined:
    Nov 9, 2008
    Posts:
    9
    Yes, I had the same thought too.
     
  5. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Have a look here:

    http://unity3d.com/support/documentation/ScriptReference/Application.html

    With additive you can have your main scene with music persistent (Level A) and load then additive your game levels (Level B-X). A would stay open all the time as this is what you asked for, have persistent objects and stuff. To remove after playing Level B there is yet no automatic way when you use LoadAdditive, you need to manually keep track and delete the objects you loaded with Level B-X.
     
  6. smith88

    smith88

    Joined:
    Nov 9, 2008
    Posts:
    9
    Thanks. I'll give it a try. I'm already tracking all the objects in a scene so I can destroy them before leaving the scene. So, it shouldn't be too difficult to modify what I have.
     
  7. IPete

    IPete

    Joined:
    May 15, 2008
    Posts:
    414
    I am not sure if this helps but I have a small platform style game I am developing for iPhone. I needed a number of small animated and scripted levels. I use a prefab for each of my levels, so I just look at the level number and load the associated prefab in to the main game framework. My main game code simply adapts to variable settings set at level load.

    This allows me to have the same music playing all the time - although it still stutters a little whilst the prefab is loading. The loading is very quick and all you have to do is to destroy the instanced prefab on level end.

    Let me know if you need code help to achieve this, as I got help off the IRC Chat to help me with a few issues I was having (I kept removing the actual prefab from my game by destroying the wrong thing!).
     
  8. smith88

    smith88

    Joined:
    Nov 9, 2008
    Posts:
    9
    IPete2,

    Thanks. I believe I understand how you're doing it. I'll try it in my next game. I've already submitted this game.

    How did you deal with transitioning from menu screens to game screens, though? This was where most of my problems were. I allow the user to move from screen to screen, even mid play - to change settings, to pause, etc.
     
  9. hrissan

    hrissan

    Joined:
    Mar 9, 2009
    Posts:
    32
    Dear guys and girls! Why do you always keep asking 2 or more completely different questions in 1 thread? :)

    So the first one is easy, it is covered in docs and on the forum.
    Q1. How do you keep music object from destroying between loading different levels.
    A1. Like with any other object, you just call DontDestroyOnLoad(this); in your Start() or Awake()

    But what about the second one?

    Q2. The music shutters while the next level is loaded! It event stops completely for several seconds!
    A2: ???
     
  10. Milo_del_mal

    Milo_del_mal

    Joined:
    Jan 27, 2013
    Posts:
    43
    I know this is an old post...

    Try loading async and unloading async.