# Unity audio trigger

**URL:** https://discussions.unity.com/t/unity-audio-trigger/614209
**Category:** Audio & Video
**Tags:** Audio-Video
**Created:** [January 26, 2016, 1:52pm UTC](https://discussions.unity.com/t/unity-audio-trigger/614209 "2016-01-26T13:52:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![J-FENG](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@J-FENG](https://discussions.unity.com/u/J-FENG)
#### Post date: [January 26, 2016, 1:52pm UTC](https://discussions.unity.com/t/unity-audio-trigger/614209/1 "2016-01-26T13:52:16Z")

</div>

Hello  
I meet some problem in setting audio trigger. Now, i have 2 cubes, and i give these 2 cubes different audio. i want when FPS controller go to one cube, the audio start, and when FPS controller leave the cube, the audio still keeping playing until FPS controller touch the second cube, and it stop playing the first cube audio, start playing second audio

#pragma strict  
var st:AudioClip;  
var se:AudioClip;  
GetComponent.().mute =true;

function OnTriggerEnter (others:Collider) {  
GetComponent.().mute = false;  
if(others.tag==“player”){  
GetComponent.().PlayOneShot(st);  
}  
else{

if(others.tag==“Finish”){  
GetComponent.().Stop()  
GetComponent.().PlayOneShot(se);  
}

}

}  
Here is code i wirte…the problem i meet now is , when i touch the second cube, the second audio start playing, but the first one still here, it should be closed…

Can some one give me some advice on code ？

---

<div class="post-metadata">

### Author: ![Hyblademin](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hyblademin/32/560808_2.png) [@Hyblademin](https://discussions.unity.com/u/Hyblademin)
#### Post date: [January 26, 2016, 4:27pm UTC](https://discussions.unity.com/t/unity-audio-trigger/614209/2 "2016-01-26T16:27:21Z")

</div>

```javascript
GetComponent.<AudioSource>().mute =true;

```

What is this for? Is this statement outside of a function? Is it supposed to be in Update()? It still won’t work that way, but I’m trying to figure out why it’s there.

If you want to change the properties of the first cube when interacting with the second one, you need some reference to the first cube object. Maybe have one in the controller script?

```javascript
private var lastCubeTouched : Collider;

function OnTriggerEnter(other : Collider)
{

     if (other.gameObject.tag == "Cube")
     {
          if (lastCubeTouched != null)
          {
               lastCubeTouched.gameObject.GetComponent<AudioSource>().Stop();
               lastCubeTouched = other;
          }
}

```

The idea is that your character controller keeps track of the cube that is currently playing so that it can stop playback when it touches another one.

You might want to add a condition in there to check whether other == lastCubeTouched and in the cube script to check whether audioSource.isPlaying before performing these actions if you want to prevent the audio from restarting after touching the same cube more than once.

---

<div class="post-metadata">

### Author: ![J-FENG](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@J-FENG](https://discussions.unity.com/u/J-FENG)
#### Post date: [January 27, 2016, 8:41am UTC](https://discussions.unity.com/t/unity-audio-trigger/614209/3 "2016-01-27T08:41:14Z")

</div>

> [@Hyblademin](#):
>
> ```javascript
> GetComponent.<AudioSource>().mute =true;
> 
> ```
> 
> What is this for? Is this statement outside of a function? Is it supposed to be in Update()? It still won’t work that way, but I’m trying to figure out why it’s there.
> 
> If you want to change the properties of the first cube when interacting with the second one, you need some reference to the first cube object. Maybe have one in the controller script?
> 
> ```javascript
> private var lastCubeTouched : Collider;
> 
> function OnTriggerEnter(other : Collider)
> {
> 
> if (other.gameObject.tag == "Cube")
> {
> if (lastCubeTouched != null)
> {
> lastCubeTouched.gameObject.GetComponent<AudioSource>().Stop();
> lastCubeTouched = other;
> }
> }
> 
> ```
> 
> The idea is that your character controller keeps track of the cube that is currently playing so that it can stop playback when it touches another one.
> 
> You might want to add a condition in there to check whether other == lastCubeTouched and in the cube script to check whether audioSource.isPlaying before performing these actions if you want to prevent the audio from restarting after touching the same cube more than once.

haha…yeah…i put outside of the function, because i want when u start program, there is no audio sound unitl u touch the trigger.

---

<div class="post-metadata">

### Author: ![Hyblademin](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hyblademin/32/560808_2.png) [@Hyblademin](https://discussions.unity.com/u/Hyblademin)
#### Post date: [January 27, 2016, 11:26am UTC](https://discussions.unity.com/t/unity-audio-trigger/614209/4 "2016-01-27T11:26:42Z")

</div>

I can see your logic there. Instead, put that statement inside of the Start() function, which is called automatically from each script right when they start (the first frame they are active), before any update.

```javascript
Start()
{
     GetComponent<Audiosource>.mute = true;
}

```

Have you had any luck with the rest?
