- Home /
The question is answered, right answer was accepted
Music code not playing music.
I'm trying to play music fluently between my 'MainMenu' and 'Settings' scenes but the code I have wont play the music, it doesn't show any errors and I can't find anything wrong with the code.
Code:
//Keeps the music playing fludently throughout 'MainMenu' and 'Settings'
static bool AudioBegin = true;
void Awake()
{
if (!AudioBegin)
{
Debug.Log("Music");
GetComponent<AudioSource>().Play();
DontDestroyOnLoad(gameObject);
AudioBegin = true;
}
}
void Update()
{
if (Application.loadedLevelName == "MainScene")
{
Debug.Log("Music2");
GetComponent<AudioSource>().Stop();
AudioBegin = false;
}
}
So the purpose of your $$anonymous$$ainScene is to stop the music ? And your SettingScene plays the music only if it's loaded after the $$anonymous$$ainScene ? That's what I'm reading.
"I'm trying to play music fluently between my '$$anonymous$$ain$$anonymous$$enu' and 'Settings'" So you play a $$anonymous$$usic in your scene, and when you go to the scene settings, your music keeps playing normally. That is what I understand. Can you tell me if I am write or wrong in my understanding. Because I made a project that has this feature.
@Blue Cut Yeah, so I want the music to play fluently between the $$anonymous$$ain $$anonymous$$enu and Settings but when I start the game I want it to start playing different music. I also have a volume slider that I want to control the volume of the music throughout the whole game but I have the code for the volume slider already I just need help on the fluent music part. But yes, you're correct.
Answer by Murkas · Apr 14, 2016 at 07:13 PM
The only point where music could be played is within the Awake-function. This function is called only once. Since your AudioBegin-Boolean is set to "true" at the beginning it will never play.
You should take a look at Unity's Event execution order. I think using "OnLevelWasLoaded" could help. Using "Update" is not that good, since it is called ever frame. You don't want to stop your music every frame.
@$$anonymous$$urkas I'm attempting to play music on my $$anonymous$$ain $$anonymous$$enu and Settings Scenes, not my '$$anonymous$$ainScene'(Sorry, I wasn't clear about that) so it would be correct to stop the music on my '$$anonymous$$ainScene'.
Answer by churu · Apr 14, 2016 at 07:59 PM
Well.... Someone would be Try just creating a audio source
Public audioclio namehere;
audiosource secondname;
void Start() { secondname= GetComponent(); secondname.clip = namehere;
}
void yourstuffhere() { namehere.play()
}
Then use the loop function unity offers https://i.gyazo.com/903b5cb184d04c32e70de5fe6f4e3fdb.png
to stop playing the music simply go with namehere.pause()
Im just a beginner myself but this could be something to experiment with.
Answer by Blue-Cut · Apr 14, 2016 at 08:03 PM
You should take a look at this : https://docs.unity3d.com/ScriptReference/AudioSource-time.html
So you have 3 scenes : - MainMenu => music1 - Settings => music1 - MainScene => music2
What you need to do basically is :
void Start()
{
GetComponent<AudioSource>().Play();
}
But you should not try to keep your gameobject alive between the scenes with music1 and the scene with music2. First of all, just do a script that plays the music at Start, and set the correct audioSource in each scene.
Having music1 playing fluently between your 2 scenes is a next step.
I think that in my project, we have dealed with the time. If you quit your scene at time=4 you begin the music at time=4 in the next scene.
I am not sure about my project, if your problem is not too urgent or if you can't find a solution, I can check that tomorrow. But dealing with objects that stay alive from one scene to another can be a bit tricky I guess, that is not something I recommand if you can avoid doing this.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need Help With Music 0 Answers
C# DontDestroyonLoad How? 0 Answers