Alternative solution for playing sound FX

Hi all! First post here -

I actually went ahead and tried to setup the sound FX before this lesson as a personal challenge. I came up with something quite different from the video but it seems to work pretty well.

I am posting this because I’d like to first get feedback just in case is a bad implementation, and second, to share it in case it is a good one :slight_smile:

Instead of attaching sounds to the individual objects like the ball or the brick, I created a SoundManager (just like our LevelManager) that I turned into a prefab that I can copy over with the rest of the prefabs when creating new levels.

This SoundManager loads the sounds on Awake and has a public method play(Object o) that can be called from Ball or Brick for example. All you need to do is pass this, the instance from where you make the call.

Code here https://gist.github.com/kennethpino/0202833eec7cc098012e8d4ac4fef98e

At the beginning, SoundManager had two methods to play the sounds: one for the ball and one for the brick. Later I decided to add the switch and play the appropriate sound based on the object to make it more flexible for future additions.

From my POV, the advantage of doing this is that it is all scripted, and later on I do not have to re-attach sound files to the sound components to each individual brick or ball prefabs. Like this is all ready to go.

Voila :slight_smile:

1 Like

This is pretty cool, I can’t say much to whether it’s bad implementation, but I think at this level as long as it works, it’s great!

I was thinking about adding some sort of sound effect whenever the ball hits the sides as well. I was trying to think of how to do that. I suppose I could add a tag to the boxes on the sides and tops and call it “Boundaries,” or something like that, and have the “OnCollider” method call a function to test whether it’s hitting the boundaries or the paddle.

I would probably change “o” to a string and it send it that way, instead of converting from an object. It seems like that would be easier or more readable to me, but that’s just me.

Privacy & Terms