Continue playing music ..?

Hi

I want the music continues playing when you die or start the same level again using “LoadLevel” script… I tried “DontDestroyOnLoad” but it started to duplicate the music!!

Notes: My game is 2D and i’m using java for scripting

You can create a bool variable and use to block some change that you don’t want in your music… Like:

C#
using UnityEngine;
using System.Collections;

public class NameOfTheScript
{

   bool Music = false;
   ...
   if (Music == false)
   {
      audio.play();
      Music = true;
   }
   ...
}

:wink:

^^^^
Sorry but it doesn’t work for me and please I need a java script !!

The language is UnityScript (aka JavaScript), not Java. Maybe the reason people get confused about this is that they confuse ‘JavaScript’ with ‘Java script’, but the language is not in fact Java (which isn’t supported in Unity).

Regarding the ‘don’t destroy on load’ problem, one solution is to create the object in a separate scene that precedes the scene in question, so that it’s not recreated every time the scene is loaded. (There are other possible solutions as well, of course, such as creating a ‘music player’ object of some sort that creates a persistent object on demand, and only when needed.)

yeah I think that is the solution but please could you give me the script … ??

There is no ‘the script’. What part are you unsure of exactly? If you can narrow down your question a bit, I’m sure someone will be able to help.

^^^^^^^
let me clear one thing for you my English isn’t good enough to talk a lot >.>

so i need any script other than “DontDestroyOnLoad” can solve my problem.??

Your English is fine :slight_smile: My comment regarding ‘the script’ is simply in reference to the fact that people often ask for ‘the script’ to do something or other, when there really is no such thing. (Usually there’s multiple ways to solve any given problem, and it often involves multiple modules working together rather than just a single script.)

The idea is to create an object in a scene that’s loaded prior to your other scenes, and mark it as ‘don’t destroy on load’. Alternatively (and this might be preferable, especially when working in the editor), you can write code that creates a game object marked ‘don’t destroy on load’, but only on demand and only when it’s not already in existence.

@Jesse Anders … thank you very much my problem is finally solved and just as you said the solution is to create an object and attached it with your music and “Don’tDestroyOnLoad” .

and of course The must important thing is to put the object in a

.

شكراً mean thanks btw (;

You’re welcome :slight_smile: