Simple-Unity-Audio-Manager icon indicating copy to clipboard operation
Simple-Unity-Audio-Manager copied to clipboard

`ArgumentOutOfRangeException` when calling `JSAM.AudioManager.PlaySound`

Open bopace opened this issue 5 years ago • 2 comments

i ran into an issue trying to call JSAM.AudioManager.PlaySound directly through script. i have four sound files added to an audio manager, and i'm using the enum value of the sound i want to play (JSAM.Sounds.MySound, which in this case happens to be value 3). it throws an ArgumentOutOfRangeException, and i've included the stack trace at the end of this issue in case it's of use to you.

through some console logging i found that when accessed in PlaySound, instance.audioFileObjects only has a single element, the first sound i added to the audio manager. no wonder it's throwing the exception... my audio file isn't in the instance array. note that creating and referencing an Audio Player component allows me to play the sound just fine using AudioPlayer.Play, and i suspect this is because it calls PlaySoundInternal which accesses audioFileObjects instead of instance.audioFileObjects. maybe the instance isn't getting set up properly?

i fully believe this could be user error as well. i created a prefab from AudioManager and i'm pulling it into multiple scenes. is this an unexpected usage of the audio manager? i verified that all audio files are added on the prefab (and the individual scene instances), and everything looks like it's in order.

lemme know if there's anything i can do to help you repro. and maybe when saturday rolls around i'll have time to dig in a little deeper and investigate further.


ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <fb001e01371b4adca20013e0ac763896>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <fb001e01371b4adca20013e0ac763896>:0)
JSAM.AudioManager.PlaySound[T] (T sound, UnityEngine.Transform trans) (at Assets/Jacky's Simple Audio Manager/Scripts/AudioManager.cs:1507)
PlayerMovement.FixedUpdate () (at Assets/Scripts/PlayerMovement.cs:70)

bopace avatar Nov 12 '20 06:11 bopace

Hi bopace, sorry to see that JSAM has been giving you some issues.

The prefabbing and usage of the AudioManager across multiple scenes should be supported to the extent that each scene that shares the AudioManager also uses the same pool of Audio Files.

The issue with instance.audioFileObjects and audioFileObjects can happen due to the nature of JSAM operating on a Singleton pattern in the editor, a design pattern that I hope to eventually move JSAM away from for version 3. JSAM's implementation should be quite robust, but I suppose that there are still edge cases to be accounted for.

For the time being, a temporary fix would just be to close and re-open the editor. If that doesn't work, destroy your scene's AudioManager gameobject, and add it back in.

To help me reproduce the bug, do reply with your Unity editor version, your JSAM version, the number of scenes you're sharing the AudioManager as well as the steps you take to reproduce the bug on your end. If you're at all comfortable with it, attaching a package containing the bare necessities needed to reproduce your issue would go a long way.

Sorry again for not getting back sooner but hopefully this helps!

jackyyang09 avatar Nov 15 '20 03:11 jackyyang09

Heya. I had a very similar issue, and had a prefab with the AudioManager. I believe the prefabbing is the issue.

Once I started the game in the editor, the list of audio file objects got slightly changed - In a way that wouldn't let me make persisting changes, no matter how many times I regenerated the audio library. BUT - After attempting to unpack the prefab instance, regenerating the audio library, and entering play mode again - It worked! The latest change to the library persisted!

I don't know what ot make out of it, but it might help direct towards the solution. (Also, I hope I'm not reviving a dead issue of a dormant project? If I am, then it's no biggie.)

blawnode avatar Apr 22 '23 19:04 blawnode