Downsides to using Audio Source component?

This is my first post here in the community. I’m really enjoying the courses so far.

While doing the challenge for the Laser Defender “Trigger Sound Effects” lecture, I decided to add an Audio Source component for my Laser prefab instead of writing the variable into Player.cs. I figured it’d keep the code cleaner, and I can control the volume in Unity anyways. Are there any downsides to doing it this way? Only one I can think of is that I’d have to select the Laser prefab to tune instead being able to do it all under the Player prefab.

Hi JackK,

Welcome to our community! It’s great to hear that you are enjoying the videos so far. :slight_smile:

In many cases, there are multiple ways to make something work in Unity. If your code works, it’s a solution by definition. Some things are a matter of personal preference. Is the laser able to make a sound? If so, it makes sense to give it an AudioSource. We would give a “Dog” an AudioSource as well to make it bark even though we could play the barking sound on the player or somewhere else. The player would not notice any difference but for us, the game developers, components on unexpected game objects could our lives make unnecessarily difficult, especially in larger games.

To evaluate whether your approaches are generally good, ask yourself two questions:

  1. Does my code work?
  2. Does the implementation make sense so my future self will be able to understand what I did today?

If you are able to answer both questions with “yes”, you are tendentially on the right track.

Hopefully, this helped. :slight_smile:


See also:

1 Like

@Nina can you explain in more details the pros and cons of each one?

What details would you like to know?

That answers my question. Thank you, Nina!

This course is my first experience with coding, and everything seems so precise (which I like) that it has me second guessing when I don’t go exactly by the lesson.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms