Cancelling audio before starting new audio
I almost have a working radio, where the player can walk up to it, press a button and cycle through songs. Everything works except for that with each button press the new song will play but the original will continue to play underneath it. For each new song that is played a new object is created but they are all called 'One Shot Audio,' so I don't know how to destroy them. If I can fix this bug then this should be a useful radio script for anyone who wants to use it. Here's the script attached to the player, thanks!:
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "radio") {
radioEnter = true;
}
}
void OnTriggerExit2D(Collider2D target){
if (target.gameObject.tag == "radio") {
radioEnter = false;
}
}
public void radioUse(){
if ((Input.GetKeyDown (KeyCode.M)) && song3on == true && radioEnter == true) {
AudioSource.PlayClipAtPoint (song1, transform.position);
song1on = true;
song2on = false;
song3on = false;
}
else if ((Input.GetKeyDown (KeyCode.M)) && song1on == true && radioEnter == true) {
AudioSource.PlayClipAtPoint (song2, transform.position);
song1on = false;
song2on = true;
song3on = false;
}
else if ((Input.GetKeyDown (KeyCode.M)) && song2on == true && radioEnter == true) {
AudioSource.PlayClipAtPoint (song3, transform.position);
song1on = false;
song2on = false;
song3on = true;
}
}
public void Update(){
raidoUse();
}
Just an update, don't know if this helps, but I got this response from StackO since no one was responding here, and it says this, but it gets as far as the AddComponent line, which immediately appears in red:
The following code is not tested.
Public class Radio : $$anonymous$$onoBehaviour
{
AudioSource output;
public AudioClip[] songs;
int songIndex = 0;
void Start(){
output = AddComponent<AudioSource>();
}
public void ToggleSong(){
songIndex++;
output.clip = songs[songIndex % songs.Length];
output.Play();
}
public void TurnOn(){
ToggleSong();
}
public void TurnOff(){
output.Stop();
}
fix the compile errors first and test it.
the first "public"needs to be lower case
at the end there's a braceright missing
Answer by simplicitydown · Jan 04, 2016 at 08:31 PM
Here it is, working well in completion:
public bool song1on = false, song2on = false, song3on = true;
public bool radioEnter = false;
AudioSource output;
public AudioClip[] songs;
int songIndex = 0;
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == “radio”) {
radioEnter = true;
}
}
void OnTriggerStay2D(Collider2D target){
if (target.gameObject.tag == “radio”) {
radioEnter = true;
}
}
void OnTriggerExit2D(Collider2D target){
if (target.gameObject.tag == “radio”) {
radioEnter = false;
}
}
public void radioUse(){
if ((Input.GetKeyDown (KeyCode.E)) && song3on == true && radioEnter == true) {
TurnOn ();
song1on = true;
song2on = false;
song3on = false;
}
else if ((Input.GetKeyDown (KeyCode.E)) && song1on == true && radioEnter == true) {
TurnOff ();
song1on = false;
song2on = true;
song3on = false;
TurnOn();
}
else if ((Input.GetKeyDown (KeyCode.E)) && song2on == true && radioEnter == true) {
TurnOff ();
song1on = false;
song2on = false;
song3on = true;
TurnOn ();
}
}
private void Start()
{
output = gameObject.AddComponent<AudioSource>();
}
public void ToggleSong(){
songIndex++;
output.clip = songs[songIndex % songs.Length];
output.Play();
}
public void TurnOn(){
ToggleSong(); }
public void TurnOff(){
output.Stop();
}
private void Update()
{
radioUse ();
}
Your answer
Follow this Question
Related Questions
Audio doesn´t mute when i use my slider 0 Answers
Soundarray for player is not working properly 1 Answer
Unity noob here, need help adding footstep logic 1 Answer
How to destroy object after it moves out of screen 7 Answers
[Solved] Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first ? 2 Answers