- Home /
Duplicate Question
Audio script doesn't work
Hi everyone so i have this script which for some reason doesn't work. What i want the script to do is just play a ambient sound troughout all of the scenes expect for 1 specific scene. Like i said it doesn't work for some reason this is my script
function Awake () { DontDestroyOnLoad (transform.gameObject);
if(Application.loadedLevel) == ("BehindLocks") audio.Stop(); }
Do you have an audio source attached to the game object containing this script ?
You never play the audio, you only stop it. Is it normal ?
You say that the script does not work, tell us what it should do, and what it is actually doing.
Yes i have an audio source atached to the game object which is containing the script. i have the audio playing on awake so every time i start the game the audio starts playing. The thing that is wrong is that unity says that there is an unexpected token "==" and it says ";" expected insert a semicolom at the end. The script should play a sound troughout the whole game expect for a particular scene : the "BehindLocks" scene
You will see that your code is erroneous when you format them correctly.
function Awake () {
DontDestroyOnLoad (transform.gameObject);
// 2 mistake:
// 1. $$anonymous$$arked by the huh down below, wrong syntax
// 2. Application.loadedLevel is an int
if(Application.loadedLevel) == ("BehindLocks") <- Huh?
audio.Stop();
}
Correction:
function Awake () {
DontDestroyOnLoad (transform.gameObject);
if( Application.loadedLevelName == "BehindLocks" )
audio.Stop();
}
for some reason when i enter the "BehindLocks" scene the sound doesn't stop but just keeps playing for some reason Thanks eventough it didn't work
Follow this Question
Related Questions
How to play several audio cllips one after another. 3 Answers
Audio after Audio Loop 1 Answer
Play audio on keyboard click? 1 Answer