# Play sound on megaphones in unity

**URL:** <https://qa.fmod.com/t/play-sound-on-megaphones-in-unity/12468>\
**Category:** Unity\
**Created:** [April 28, 2016, 12:05am UTC](https://qa.fmod.com/t/play-sound-on-megaphones-in-unity/12468 "2016-04-28T00:05:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PrimeJoker](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@PrimeJoker](https://qa.fmod.com/u/PrimeJoker)\
**Post date:** [April 28, 2016, 12:05am UTC](https://qa.fmod.com/t/play-sound-on-megaphones-in-unity/12468/1 "2016-04-28T00:05:33Z")

</div>

Hi guys,  
I’m new to fmod and Unity. Would like to know how to set 3D sound at random located e.g. 3 megaphones and then when the alarm is triggered to play an alarm sound? I have tried to read up on attributes3D though don’t get it. Any help will be appreciated.

---

<div class="post-metadata">

**Author:** ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)\
**Post date:** [May 2, 2016, 6:01am UTC](https://qa.fmod.com/t/play-sound-on-megaphones-in-unity/12468/2 "2016-05-02T06:01:40Z")

</div>

Once you have the (X, Y, Z) values in the Attributes3D object you would call [EventInstance.Set3DAttributes](http://www.fmod.org/documentation/#content/generated/FMOD_Studio_EventInstance_Set3DAttributes.html)

---

<div class="post-metadata">

**Author:** ![PrimeJoker](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@PrimeJoker](https://qa.fmod.com/u/PrimeJoker)\
**Post date:** [May 23, 2016, 9:33pm UTC](https://qa.fmod.com/t/play-sound-on-megaphones-in-unity/12468/3 "2016-05-23T21:33:50Z")

</div>

> [@](#):
>
> Once you have the (X, Y, Z) values in the Attributes3D object you would call [EventInstance.Set3DAttributes](http://www.fmod.org/documentation/#content/generated/FMOD_Studio_EventInstance_Set3DAttributes.html)

Thanks Nicholas Wilcox, this is kind of the solution I went with which worked. The only problem is that when the level restarted the alarm were playing in the background. Any ideas what I need to do.

using UnityEngine;  
using System.Collections;  
using FMOD.Studio;

public class TurnAlarmOn : MonoBehaviour  
{  
private AlarmLight alarm;  
private PlayerHealth PlayerHealth;

```
//--define the event instance for the alarm sound
private EventInstance alarmRingEv;

//--define playback state for the alarm sound
private PLAYBACK_STATE playBack_stateAlarm;

//--define 3D attributes for 3D sound to emit
private ATTRIBUTES_3D soundEmit;

//--define transform objects which this script will be attached to
public Transform sirenObj;

void Start()
{
	//--setup the reference to the alarm light.
	alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();

	//--obtain Vector3 coordinates for the megaphones located around the Stealth scene
	soundEmit.position.x = sirenObj.transform.position.x;
	soundEmit.position.y = sirenObj.transform.position.y;
	soundEmit.position.z = sirenObj.transform.position.z;

	//--lookup for the alarm sound file
	alarmRingEv = FMOD_StudioSystem.instance.GetEvent("event:/alarmSound");

	//--emit the alarm sound as 3D
	alarmRingEv.set3DAttributes(soundEmit);

	//--condition to stop the alarm if it is playing
	if(playBack_stateAlarm == PLAYBACK_STATE.STOPPED)
	{
		alarmRingEv.stop(STOP_MODE.IMMEDIATE);
		Debug.Log ("Stop alarm");
	}
}

void Update()
{
	alarmRingEv.getPlaybackState(out playBack_stateAlarm);

	//--condition to start the alarm if its not playing, if its has not stopped
	if(alarm.alarmOn && playBack_stateAlarm != PLAYBACK_STATE.PLAYING)
	{
		alarmRingEv.start();
		Debug.Log ("Start alarm");
	}
	else if(!alarm.alarmOn && playBack_stateAlarm != PLAYBACK_STATE.STOPPED)
	{
		alarmRingEv.stop(STOP_MODE.IMMEDIATE);
		Debug.Log ("Stop alarm");
	}
}

```

}

---

<div class="post-metadata">

**Author:** ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)\
**Post date:** [May 24, 2016, 1:33am UTC](https://qa.fmod.com/t/play-sound-on-megaphones-in-unity/12468/4 "2016-05-24T01:33:44Z")

</div>

GetEvent is returning you a new instance that is different to the one that was playing previously. I would recommend using the the Unity OnDisable or OnDestroy functions to stop the audio.
