To make a sound manager

Hi,

I’ve been trying to make a sound manager that I can just drag and drop (or better, without even manually putting in a level) that would “just work”. My game is a simple 2D one and I prefer to just call a static function and hear a sound effect.

I know about singleton design pattern, my problem is where to put the sound effects.

If I put them on the character and pass them to the sound manager, if something else wants to trigger this sound effect, needs to call player to do so (or have it’s reference which causes redundency as you have to update 2 places everytime you want to change that particular sound)

If I put the sounds in the sound manager, the way I prefer to do so, and store it as a prefab, it loses it’s references as prefabs do not hold references.

The only way I could find so far is to address files from the project directory structure (Assets/sfx/…) but it’s very fragile and can cause runtime error if you are not careful enough.

Any ideas?

You can use Resources.Load if you place your audio files in a a resources folder.

And @Trisibo made a really nice utility to keep references to assets in the inspector

Why don’t you put them in the sound manager? No need to use prefabs. It’s the simplest solution, and every time you want to update/change a sound effect, you’ll know exactly where to find it - you won’t have to waste any time looking through your whole project.

I recommend using [SerializeField] on each sound so you can drag & drop in the inspector, and then use DontDestroyOnLoad on your sound manager object so that sounds are only loaded once when your game starts up.

I highly recommend you visit this Unity tutorial: https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system

Start using an event system now throughout your game, and reduce unnecessary pain and frustration later. Using events for your sound manager is a great way to start. :slight_smile:

As I mentioned, this makes everything brittle as you can’t change directories of files.

Which sound manager?! I’m trying to find out how to write one!

Event system is not performant when you want to have 1 sound / second, for example for a machine gun. At least that’s what I’ve read before.

The “sound manager” can just be a singleton that plays sound effects.

Sending an event once every second will be so negligible, it’s not going to have a measurable impact on your overall performance. If you were sending 1000’s of events per second, then you might have reason to worry. If you had a sound that repeated indefinitely (for example, a machine gun) you could use two events (machine_gun_start and machine_gun_end) and just loop the sound. But I wouldn’t worry about optimizing your sound manager when you haven’t even written one yet. :slight_smile:

1 Like

What do you mean prefabs don’t hold references?

They do just fine.

They just can’t references to scene objects… because the scene isn’t guaranteed to exist wherever the prefab exists. Where as assets all exist just fine.

Also… if you really want a Singleton… I’d suggest doing a ScriptableObject, putting it in a Resources folder, and making that the singleton.

Again though… not sure what you mean it doesn’t hold references…

@faraza158

I’m not against tutorials at all, but is it actually fair to necro 10 or more threads in small amount of time to market your (?) tutorial videos…

1 Like

He’s a plain old spammer. Don’t try to reason, just report and move on

1 Like