Audiokinetic's Community Q&A is the forum where users can ask and answer questions within the Wwise and Strata communities. If you would like to get an answer from Audiokinetic's Technical support team, make sure you use the Support Tickets page.

Unity: Getting an error when I stop a looping sound and destroy the GameObject on the same frame

0 votes

There is a flying insect in my game that, when it spawns, begins emitting a looping sound.

When the bug is supposed to die, I post the Wwise event that stops its looping sound, and then call Destroy(gameObject) as the next line of code.

When I do this, I get this error: Wwise: Unknown game object ID. Make sure the game object is registered before using it and do not use it once it was unregistered.: 1

How can I stop the looping sound being emitted from that gameobject, and destroy the gameobject on the same frame? I've been using Wwise for over a year now and I'm surprised that this is an issue and that I didn't run into this issue until now.

Thanks a lot for any help!

 

asked Aug 3, 2021 in General Discussion by James M. (800 points)

1 Answer

+1 vote
As I recall it, the execution order of script is such that wwise executes after normal unity code, meaning, that even if you write it like

-stop sound

-destroy object

the -stop sound- runs after the object gets destroyed even if you did not write it like that.

I can't recall what the syntax is named, but I'm pretty sure I recall that there is a tool in the API for executing wwise commands instantly, if you really wish to have it all in the same frame, and not just waiting for the next frame, before destroying the object.
answered Aug 6, 2021 by Lisbeth R. (250 points)
edited Aug 8, 2021 by Lisbeth R.
Thanks, yeah that does appear to be how it works - I think the audio stuff may be running in a separate thread, so I suppose it makes sense that my commands might get queued up and then executed later.

But even if this was the case, why does it know that the gameobject is destroyed BEFORE it executes my Stop command? Just seems strange.

I also tried the 'wait until next frame before destroying object' approach, and it did not work (same error). If I wait longer (like 0.1s), it did work, so that is what I'm doing right now (disabling the gameobject, stopping the sound, waiting 0.1s, then destroying). It feels pretty janky to do it that way, so I'm not super happy about it and would love for a cleaner way to do this.
I'm pretty sure Wwise simply check whether the object is there or not, before it tries to execute its code. Just like a regular Null error from C# when something is not there, but was supposed to be there. In any case, you can try to unregister the object such that the code would look something like:

AkSoundEngine.StopAll (gameObject);
AkSoundEngine.UnregisterGameObj(gameObject);
StartCoroutine(Wait);

Ienumerator Wait(){
   yield return null
   Destroy(gameobject);
}

Also, I checked up on when the different things are running, and it goes something like:

start of a frame:
your C# script runs
then the graphics are rendered

End of frame:
wwise code runs
...