# Managing Multiple Instances of Same Event

**URL:** <https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [February 28, 2024, 8:35am UTC](https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297 "2024-02-28T08:35:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shennoz](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/shennoz/32/6277_2.png) [@Shennoz](https://qa.fmod.com/u/Shennoz)\
**Post date:** [February 28, 2024, 8:35am UTC](https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297/1 "2024-02-28T08:35:47Z")

</div>

Hi! This is a two-part question.  
I have an event that I call multiple times, and each time I’d make a unique instance and add it to a list.

1. If I’d like to stop and release one specific instance (that hasn’t played till the end), how can I filter it out? From what I understand, all instances will share the same ID and Event Description. Is there something in FMOD or do I need to implement my own way of discriminating each instance?

2. When cleaning up the list from the instances that got released. Is it enough to use `instance.isValid()` and remove it when the return is false?

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [February 29, 2024, 3:01am UTC](https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297/2 "2024-02-29T03:01:50Z")

</div>

> [@Shennoz](#):
>
> If I’d like to stop and release one specific instance (that hasn’t played till the end), how can I filter it out? From what I understand, all instances will share the same ID and Event Description. Is there something in FMOD or do I need to implement my own way of discriminating each instance?

FMOD offers some options related to limiting event instance counts, but beyond that, if you want to stop/release an instance for reasons specific to your game, then you will need to implement a way of discriminating between them in your own code.

> [@Shennoz](#):
>
> When cleaning up the list from the instances that got released. Is it enough to use `instance.isValid()` and remove it when the return is false?

Yes, it is enough to use `instance.isValid()`. Another option, assuming that the event has a natural end and you no longer need access to it, would be to release the event and then immediately remove it from your list, since the system will handle cleaning it up automatically when it stops playing.

---

<div class="post-metadata">

**Author:** ![Shennoz](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/shennoz/32/6277_2.png) [@Shennoz](https://qa.fmod.com/u/Shennoz)\
**Post date:** [February 29, 2024, 1:37pm UTC](https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297/3 "2024-02-29T13:37:42Z")

</div>

Thanks for the quick reply!

> then you will need to implement a way of discriminating between them in your own code.

This confirms it then, thank you! I’ve noticed there’s also the option to add some additional data to an instance using `instance.setUserData()` which I thought about using to tell instances apart. Although I’m unsure if it’d be performant when used a lot to filter a list of many instances.

> Another option, assuming that the event has a natural end and you no longer need access to it, would be to release the event and then immediately remove it from your list

I have some events that are entirely a loop region and from some testing I saw that FMOD won’t actually release these events unless I manually stop them (even if I call release right after start, which makes sense). So I guess these are the ones that go against the “natural end” you’ve mentioned - also why I need to access the specific events based on what game object requested playing those loops.

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [March 1, 2024, 12:52am UTC](https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297/4 "2024-03-01T00:52:05Z")

</div>

> [@Shennoz](#):
>
> I’ve noticed there’s also the option to add some additional data to an instance using `instance.setUserData()` which I thought about using to tell instances apart. Although I’m unsure if it’d be performant when used a lot to filter a list of many instances.

You _could_ use user data in the way you’ve described, yes. However, because user data in C# is interacting with both managed C# (Unity) and native CPP (FMOD), you would need to add extra IntPtr and GCHandle boiler plate to handle this - you’d essentially be introducing a small aspect of memory management to your code where there’d otherwise not be any. As a result, it’s probably be more convenient to handle distinguishing between instances in your own C# code.

---

<div class="post-metadata">

**Author:** ![Shennoz](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/shennoz/32/6277_2.png) [@Shennoz](https://qa.fmod.com/u/Shennoz)\
**Post date:** [March 1, 2024, 9:11am UTC](https://qa.fmod.com/t/managing-multiple-instances-of-same-event/21297/5 "2024-03-01T09:11:43Z")

</div>

Aha! Good tip. Thanks Leah!
