- Home /
Active audiosource change on button press. Making a ingame radio.
Hello everyone! I am now working on my game, and I want to make a radio system like GTA or Saints Row in my game. For 1st try I wanted to use gameobject with attached script and 2 audiosources with audioclips. I wrote script, which on button press activates 1st source (deactivates 2nd); activates 2nd(deactivates 1st) but it's wont work. So, there is a script -
var audio1 : AudioSource;
var audio2 : AudioSource;
function Update () {
if (Input.GetKeyDown ("1"))
audio.Play(audio1);
audio.Stop(audio2);
if (Input.GetKeyDown ("2"))
audio.Play(audio1);
audio.Stop(audio2);
}
Then, I tryed another code:
var audio1 : AudioSource;
var audio2 : AudioSource;
function Update () {
if (Input.GetKeyDown ("1"))
audio1.active = true;
audio2.active = false;
if (Input.GetKeyDown ("2"))
audio1.active = false;
audio2.active = true;
}
But it's dont work too. I googled this topic, but people just made a radio stream from internet. I want to use local music from game assets for radio system, and change it with keyboard button. Thanks every one, waiting for ideas.
Answer by Madswint · Jun 12, 2014 at 04:23 PM
var audio1 : AudioSource;
var audio2 : AudioSource;
function Update () {
if (Input.GetKeyDown ("1")) {
audio.Stop(audio2);
audio.Play(audio1);
}
if (Input.GetKeyDown ("2")) {
audio.Stop(audio1);
audio.Play(audio2);
}
}
Only a year late but here you go :>
$$anonymous$$eycode.1 and $$anonymous$$eycode.2, not "1" and "2", that almost never works properly.
Your answer
Follow this Question
Related Questions
Audio clip not playing 1 Answer
Audio playing problem 1 Answer
Audio not playing... 2 Answers
Multiple audio sources; Unity won't let me attach in the inspector 2 Answers
Audio.Play() not showing up 1 Answer