- Home /
Getting sound trigger to work
I don't understand why my sound trigger is not working. I have created a GameObject and added an Audio Source to it (on Audio Source I have the 'Play On Wake' and 'Loop' checked. Then I added a Sphere Collider (I have the 'Is Trigger' checked). Finally I added my script and added the sound clips into the inspector. Unity also says that: "There are 2 audio listeners in the scene." I have pretty much unchecked all the audio listeners and it still says it, what am I doing wrong?
Here is how my script looks like:
#pragma strict
var WalkAudio:AudioClip;
var OutCry:AudioClip;
function Update (){
var Audio= gameObject.GetComponent(AudioSource);
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
Audio.Play();
}
else
{
Audio.Pause();
}
}
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var Audio= gameObject.GetComponent(AudioSource);
if (hit.gameObject.tag == "templeFloor")
{
var groundType = 1;
print("Temple");
Audio.clip=OutCry;
}
else{
Audio.clip=WalkAudio;
}
}
Answer by TheFloatingSheep · Jul 12, 2015 at 02:29 PM
The... var Audio= gameObject.GetComponent(AudioSource);
You don't need that part for the audio component. If the script is on the same gameobject you can simply use audio.Play(); audio.Pause();
If the audio component is on another gameobject, you can add a var
var Example : GameObject;
and use it like:
Example.audio.Play();
Your answer
![](https://koobas.hobune.stream/wayback/20220611230758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with timed code-execution (audioplay and object destruction) 1 Answer
Collecting objects? 1 Answer
On trigger , sound play 2 Answers
On trigger enter play sound problem 2 Answers
Ai trigger 0 Answers