# Should I create an Event Instance in code or just use the event emitter component?

**URL:** <https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664>\
**Category:** Unity\
**Tags:** csharp\
**Created:** [April 14, 2022, 10:17pm UTC](https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664 "2022-04-14T22:17:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taki640](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/taki640/32/3426_2.png) [@taki640](https://qa.fmod.com/u/taki640)\
**Post date:** [April 14, 2022, 10:17pm UTC](https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664/1 "2022-04-14T22:17:13Z")

</div>

Hi, hope you guys are doing alright. So, I have some sword sounds with the instance being created directly on code with the `FMODUnity.RuntimeManager.CreateInstance()` should I instead use the Event Emitter component? Which method is more recommended and what’s the difference between them? Also, what exactly is an _instance_?

And to anyone who read this, wish you the best.

---

<div class="post-metadata">

**Author:** ![Alcibiade](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/alcibiade/32/4284_2.png) [@Alcibiade](https://qa.fmod.com/u/Alcibiade)\
**Post date:** [April 16, 2022, 6:44am UTC](https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664/2 "2022-04-16T06:44:22Z")

</div>

An instance is a programmation term meaning basically a materialized copy of an abstract object. The object is the FMOD event, the instance is the copy created at realtime to be played. You can create several instances of a unique event, with different properties (for instance, different 3D attributes).

For the first question, I’ll let Unity users answer.

---

<div class="post-metadata">

**Author:** ![taki640](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/taki640/32/3426_2.png) [@taki640](https://qa.fmod.com/u/taki640)\
**Post date:** [April 18, 2022, 3:22am UTC](https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664/3 "2022-04-18T03:22:49Z")

</div>

Thank you for the clear explanation.

---

<div class="post-metadata">

**Author:** ![g.hadji](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@g.hadji](https://qa.fmod.com/u/g.hadji)\
**Post date:** [May 12, 2022, 11:00am UTC](https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664/4 "2022-05-12T11:00:41Z")

</div>

Wether you’ll use emitter component or code instance depends on how much you want the code to impact your sound. For example if you want to start a music event when you enter a room, you simply have to create a trigger in unity, then use the emitter component by setting the start onTriggerEnter. But let’s say you want the music to stop when there is a code condition that is met (for example when your health is below a certain threshold), it will become more efficient to use the scripted method because you’ll want your instance to react to the game’s code. In your case, I guess there is a function in your code that triggers a sword hit. I don’t believe you can access this function with the event emmiter component, so you’d have to do something like this in :

```auto

public class NameOfYourCode : MonoBehaviour
{
   //To create a reference to your FMOD Event
   private EventReference swordSfx = "the path of your event in fmod";
   //To set up a reference you'll use later in code to create your instance
   FMOD.Studio.EventInstance swordSfxInstance;

   //The function responsible for firing a sword hit
   void SwordHitFunction()
   {
     //Creating an instance from the reference of your event
     swordSfxInstance = FMODUnity.RuntimeManager.CreateInstance(swordSfx);
     //Starting the instance
     swordSfxInstance.start();
     //Allowing unity to purge to destroy the instance when the event stop
     swordSfxInstance.release();
   }
}

```

You could also use PlayOnShot() here because you don’t really need to do something else with the instance except playing it.

```auto
public class NameOfYourCode : MonoBehaviour
{
   //To create a reference to your FMOD Event, Here you don't need to manually create an instance anymore because PlayOneShot() does it for you.
   private EventReference swordSfx = "the path of your event in fmod";

   //The function responsible for firing a sword hit
   void SwordHitFunction()
   {
     //Creates an instance, plays it and releases it in one line of code
     FMODUnity.RuntimeManager.PlayOneShot(swordSfx);
     //And if you're using a 3D spatialized event, and you want the sfx to be localized on the sword
     FMODUnity.RuntimeManager.PlayOneShotAttached(swordSfx, swordGameObject);
   }
}

```

---

<div class="post-metadata">

**Author:** ![taki640](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/taki640/32/3426_2.png) [@taki640](https://qa.fmod.com/u/taki640)\
**Post date:** [May 13, 2022, 4:07am UTC](https://qa.fmod.com/t/should-i-create-an-event-instance-in-code-or-just-use-the-event-emitter-component/18664/5 "2022-05-13T04:07:56Z")

</div>

Thank you for your answer, now I understand 🙂
