- Home /
Audio not playing on activation
My previous audio stops when I press the button, but the new source does not start. Any help is is highly appreciated.
var pressed : boolean = false;
var AudioA : GameObject;
var AudioB : GameObject;
var AudioC : GameObject;
var AudioD : GameObject;
function OnMouseDown () {
audio.Play();
if (pressed == true) {
AudioA.active = false;
AudioB.active = true;
AudioC.active = false;
AudioD.active = false;
}
}
function OnMouseUp () {
pressed = true;
renderer.material.color = Color.red;
audio.Play();
}
Answer by aldonaletto · Feb 22, 2013 at 02:57 AM
You're confusing things: this script only affects its own sound, not the sounds associated to AudioA/B/C/D (unless it's attached to one of them). If you want to control the AudioA/B/C/D objects, use their audio properties instead - for instance:
AudioA.audio.Play();
But I honestly don't understand what you're trying to do: do you need to activate/deactivate objects, or it was simply a way to stop/play the sound?
No, I'm trying to stop the games sound (which works) and play and another (which doesent). The audio attached to the object works fine. I want the audio to play when it is activated from a separate game object. I have the PlayOnAwake function checked, but it won't play. Thanks for the help so far.
You can try something like this:
var pressed : boolean = false;
var AudioA : GameObject;
var AudioB : GameObject;
var AudioC : GameObject;
var AudioD : GameObject;
function On$$anonymous$$ouseDown () {
if (pressed == true) {
AudioA.audio.Stop();
AudioB.audio.Play(); // play only AudioB
AudioC.audio.Stop();
AudioD.audio.Stop();
}
}
...
PlayOnAwake only works when the object is created - activating it doesn't make the sound play.
Alright, I'll try it in he morning. Thanks for your help and sorry I misunderstood your direction the first time. I assumed you were talking about the audio.Play().
Your answer
![](https://koobas.hobune.stream/wayback/20220613093104im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script unattached to Object will not compile 1 Answer
Play Audio at the speed something is moving 2 Answers
Stop audio playing after a timer reset 2 Answers
Footstep Audio Check Collider Hitting Floor 1 Answer
Audio Script 1 Answer