# \[solved\] Unable to use callbacks in unity?

**URL:** <https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066>\
**Category:** Unity\
**Created:** [August 27, 2018, 5:30pm UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066 "2018-08-27T17:30:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dumativa](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@dumativa](https://qa.fmod.com/u/dumativa)\
**Post date:** [August 27, 2018, 5:30pm UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/1 "2018-08-27T17:30:21Z")

</div>

I’m working on a game completely consisting of sounds in unity, and I want to make use of FMOD’s callback system to identify things like music end, and beat markers, the problem is that I am completely unable to do anything from inside the callback in unity. I’ve done a lot of things to try to work around any thread shenanigans that might be happening; One of it was to simply store that the callback actually happened and doing the hard work later, in a MonoBehaviour’s update

private FMOD.RESULT Stopped(EVENT\_CALLBACK\_TYPE type, EventInstance eventInstance, IntPtr parameters)  
{  
try  
{  
var test = this == null;  
\_callbackHappened = true;  
}  
catch (Exception e)  
{  
var message = e.Message;  
}

```
    return FMOD.RESULT.OK;

```

}

What is happening is that the test bool is sometimes returning true, and the \_callbackHappened assignment throws a nullreference exception…  
Could I be wrong or using callbacks on unity isn’t supported?

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [August 27, 2018, 11:31pm UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/2 "2018-08-27T23:31:36Z")

</div>

Callbacks require the use of explicitly pinning memory so that the memory isn’t freed by the garbage collector.

We have an example on our website of how to use callbacks:  
[https://fmod.com/resources/documentation-api?page=content/generated/engine\_new\_unity/script\_example\_timeline.html#/](https://fmod.com/resources/documentation-api?page=content/generated/engine_new_unity/script_example_timeline.html#/)

---

<div class="post-metadata">

**Author:** ![Shadoninja](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Shadoninja](https://qa.fmod.com/u/Shadoninja)\
**Post date:** [August 28, 2018, 3:12am UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/3 "2018-08-28T03:12:25Z")

</div>

> [@](#):
>
> Callbacks require the use of explicitly pinning memory so that the memory isn’t freed by the garbage collector.
> 
> We have an example on our website of how to use callbacks:  
> [https://fmod.com/resources/documentation-api?page=content/generated/engine\_new\_unity/script\_example\_timeline.html#/](https://fmod.com/resources/documentation-api?page=content/generated/engine_new_unity/script_example_timeline.html#/)

Why are callbacks so tedious? The function headers and extra library calls going on here are quite dizzying. Are there any pitfalls to lookout for when making callbacks a commonplace in our games?

---

<div class="post-metadata">

**Author:** ![dumativa](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@dumativa](https://qa.fmod.com/u/dumativa)\
**Post date:** [August 28, 2018, 10:15am UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/4 "2018-08-28T10:15:31Z")

</div>

That really did the trick! It sure wasn’t as painless as I want it t be, but after encapsulating all that dizzy stuff it became really simple to work with.  
Thanks for the quick response.

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [August 29, 2018, 1:32am UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/5 "2018-08-29T01:32:29Z")

</div>

@Tanner The extra work is required when interfacing with native code (C, C++, Objective-C, etc.). [https://docs.unity3d.com/Manual/NativePlugins.html](https://docs.unity3d.com/Manual/NativePlugins.html)

As Rafael mentioned, the hard part is the initial setup.

---

<div class="post-metadata">

**Author:** ![Shadoninja](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Shadoninja](https://qa.fmod.com/u/Shadoninja)\
**Post date:** [August 31, 2018, 3:12am UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/6 "2018-08-31T03:12:47Z")

</div>

@Cameron - I got it working in my game as well this evening. I actually am attaching and removing the callbacks to/from my event in real-time as the game plays out. There doesn’t seem to be an option to delete a callback from an event once you attach it, so I am just swapping it out for an empty callback before I free the original data object the callback was writing to.

---

<div class="post-metadata">

**Author:** ![dumativa](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@dumativa](https://qa.fmod.com/u/dumativa)\
**Post date:** [August 29, 2018, 3:54am UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/7 "2018-08-29T03:54:52Z")

</div>

Only one problem though is that destroying the object as mentioned on the example while an event is playing freezes my unity editor

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [August 29, 2018, 4:06am UTC](https://qa.fmod.com/t/solved-unable-to-use-callbacks-in-unity/14066/8 "2018-08-29T04:06:19Z")

</div>

> [@](#):
>
> Only one problem though is that destroying the object as mentioned on the example while an event is playing freezes my unity editor

I believe the fix for that will be the same as posted here:

> **[GCHandle value belongs to a different domain- Error in Unity playmode -...](https://qa.fmod.com/t/gchandle-value-belongs-to-a-different-domain-error-in-unity-playmode-music-continues-to-play/)**
>
> I’m using the example script in the tutorials for unity called ScriptUsageTimeline. I’m getting this error when I stop play mode: ArgumentException: GCHandle value belongs to a different domain ...

It is due to the GCHandle being freed before the callback is actually finished with it.
