- Home /
Change (Background) Music Mid-Game
Hey,
I'm new to Unity and these forums (though not C#), and though I prefer to try find the answer myself, I'm truly stuck on this one.
Background I'm carrying on from the Space Shooter project, adding features to get some practice and it's all going very well (I have the code up on github if anyone is interested).
Problem I'm trying to create a powerup ability - nothing fancy, 3 bolts instead of 1 fire. And I want the music to change once the powerup is activated - very simple. However, no matter what I do, when the music is supposed to come on, I hear "silence" - it's actually this weird fluttering sound. Like bats or something tapping the "mic".
I've tried a few different methods but the gist is:
1) I have an AudioSource w/ background music clip attached to GameController. Music plays fine in all circumstances.
2) I have an AudioSource/ with newBackground music clip attached to a gameObject. If this is set as the initial background music, everything works perfectly. The problem occurs when the music is supposed to switch.
I've tried:
backgroundmusic.Audio.Stop();
newBackgroundmusic.Audio.Play();
=> Weird fluttering
Destroy(backgroundmusic);
newBackgroundmusic.Audio.Play();
=> Weird fluttering
Thinking that because my triggering of the sound files depends on the Update() function (at the moment, getting a score of 10 triggers it as a test), that perhaps it needs time to clear up any used objects before it can be reused.. Or something. Nope, I added a function to wait several seconds to test that. I've tried it as an .mp3 and .wav file. Also, that doesn't make sense since they both play fine simultaneously - if I set them both to Play On Awake, it works completely fine.
Finally, one weird thing. Sometimes the music actually does trigger! But at only weird times. Like after you've triggered the powerup, then died to trigger the gameOver event and then it might play. Other times I hear it starting the moment I end the game (in Unity that is).
Which makes me think that it's related somehow to an object being "tied up" with some other process. Incidentally, I tried storing the AudioSources in an array but I was having problems with that as well. One of my sound effects for the enemy ship exploding magically disappeared a few days ago so perhaps it's related...
So that's the gist of things.. I'm not new to programming but I'm far from an expert, and I'm lost.. If anyone could recommend a function/site/etc I'm sure I can figure out the rest.. Thanks guys!
Answer by RoelfMik · May 10, 2014 at 03:58 PM
Does this help? [:
Drop an AudioSource onto _AudioSource1 and drop an AudioSource onto _AudioSource2. Both AudioSources have a different AudioClip.
When I press "s" it switches between AudioSources, and plays a different AudioClip.
using UnityEngine;
using System.Collections;
public class myPlaySwitch : MonoBehaviour
{
public AudioSource _AudioSource1;
public AudioSource _AudioSource2;
void Start()
{
_AudioSource1.Play();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.S))
{
if (_AudioSource1.isPlaying)
{
_AudioSource1.Stop();
_AudioSource2.Play();
}
else
{
_AudioSource2.Stop();
_AudioSource1.Play();
}
}
}
}
You could also use one AudioSource, instead of switching between two AudioSources. When I press "s" it now switches AudioClips, not AudioSources:
using UnityEngine;
using System.Collections;
public class myPlaySwitch : MonoBehaviour
{
public AudioSource _AudioSource;
public AudioClip _AudioClip1;
public AudioClip _AudioClip2;
void Start()
{
_AudioSource.clip = _AudioClip1;
_AudioSource.Play();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.S))
{
if (_AudioSource.clip == _AudioClip1)
{
_AudioSource.clip = _AudioClip2;
_AudioSource.Play();
}
else
{
_AudioSource.clip = _AudioClip1;
_AudioSource.Play();
}
}
}
}
Hey!
Thanks for your help. I actually managed to fix it and I figured out the reason why too. I had stupidly placed the music trigger event in my gameController Update() function. $$anonymous$$eaning that however many times a second (60, 70, whatever the FPS is), it would start the audio file, leading to that flickering sound. I just moved to PlayerFiresWeapon event and it's all good!
I didn't really figure out the reason behind the enemy ship explosion sound effect not triggering, but I did solve it without needing multiple audio sources. I added the audio source to the explosion prefab and set it to "Play on awake".
Thanks again for your help though, I'll definitely be needing your code soon :)
Answer by Vice313 · Feb 27, 2017 at 11:53 AM
I have used this particular part of Code although I had to adapt naturally but The way it was laid out really helped me work it around If statments and using Void Update. Thank you so much for your insight into Audio Sources and how they work. For exsample in my text based game written for my course I have three sources on one script working in tandom at the right times.
Click ->here<- for how It was used