Alternate Audio config


Ended up refactoring as soon as i saw they were both so similar, came up with something slightly different.

Any reason from a “best practice” we wouldn’t want to have the audio clips/volumes in different scripts like this?

    public void PlayOneShotClip(AudioClip audioclip)
            AudioSource.PlayClipAtPoint(audioclip, Camera.main.transform.position, shootingVolume);

then in shooter/health component

    [Header("Audio Settings")]
    [SerializeField] AudioClip shootingSFX;
    [SerializeField] [Range(0f, 1f)] float shootVolume = 1f;

    IEnumerator FireContinuously()
            GameObject projectile = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
            Rigidbody2D rb = projectile.GetComponent<Rigidbody2D>();
            if(rb != null)
                rb.velocity = transform.up * projectileSpeed;
            Destroy(projectile, projectileLifetime);

            audioPlayer.PlayOneShotClip(shootingSFX, shootVolume);

            yield return new WaitForSeconds(GetRandomSpawnTime());



    [Header("Audio Settings")] 

    [SerializeField] AudioClip hitSFX;
    [SerializeField] [Range(0f, 1f)] float hitVolume = 1f;

    private void TakeDamage(int damage)
        audioPlayer.PlayOneShotClip(hitSFX, hitVolume);
        health -= damage;
        if (health <= 0)

Hi Caesura,

Good job on refactoring your code yourself. :slight_smile:

In many cases, there are multiple ways to make something work in Unity, and Gary and Rick cannot show all of them. If your code works, it’s a solution by definition.

Is this what you wanted to know?

See also:

Not quite!

I was really trying to leverage the expert knowledge at GameDev!

You guys are the best because generally its not just"you can do it this way" but also" here are limitations, this is cleaner, this works better for small projects"

I was hoping more for a “in my projects i would do it more like…”

Just trying to get a feel for OOP, i come from a sql background working on a warehouse management and ERP(with no schooling) so i’m always mindful of figuring out industry"best practices".

That explaination turned into a bit of a novel!

Thank you,

Thank you for your explanation. :slight_smile:

Regarding your initial question, I’m afraid that the answer is “it depends”. Since I do not know the rest of your project, it is impossible for me to tell you my opinion in this regard.

It does not matter if you are new to OOP. Your opinion is as important as everybody else’s because nobody is able to think of all advantages and disadvantages all the time. There might be good arguments for your approach.

Reading some random opinion on the internet won’t help you that much because everything depends on the context. Even “best practice” depends on the context, and you rarely find “the best” solution but usually a variety of good solutions. If Rick had created the Laser Defender project, you would probably see different solutions in this section. And if Ben had created it, his solutions would very likely be different from Rick’s and Gary’s. That’s why answering “in my projects i would do it more like…” is so difficult (for me) because my solutions might be different from Rick’s, Gary’s and Ben’s. I don’t want to give the impression that “different solution” equals to “best practice” or “this is how you must do it”.

The problem is that you often get different answers by different people. Given those people know what they are talking about, their answers are equally valid.

If you want to learn “best practices” and “get a feel for OOP”, don’t just compare the code but also your reasoning in the context of your problems because the code is usually just the result of reasoning, not the reasoning itself.

My questions are: Why did you opt for this solution? What was your reasoning? What is the advantage/disadvantage compared to Gary’s solution?

If you are able to answer these question, I could tell you if I agree or disagree.

Excellent points, though i would say that hearing different people with different answers is exactly what I like to see. I definitely like to see random opinions on the internet, whether more or less experienced, i do like to have some other frames of reference!

But to answer your questions.

  1. My first thought(i actually paused the video to fix it before Gary did) was that “both these methods are the same, can’t have that” and so i made it more generic so i could added the inputs in.

  2. My second thought was that the audio player doesn’t really need to know about the shooting or health script, it just needs to play the sound.

  3. if i was assigning a shooting SFX as a designer who didn’t code it, I’d want to set it up on the enemy or player. Possibly even the projectile if there was enough mix and match going on.

4.Avantage and disadvantage are both the same, the sounds are on the individual prefabs rather then on a centralized script, so you need to go looking for it, more work if its changing it, but also more control without needing to code a bunch of methods.

Thanks in advance Nina! Even writing them out was an excellent exercise.

Thank you. That was helpful. :slight_smile:

so you need to go looking for it, more work if its changing it

When developing a solution, I usually do that with my future self in mind. My future self is busy with other problems and is not eager to read, let alone reenact my past self’s code. Unless there is a very good reason to use a rather cryptic solution, e. g. performance issues, I prefer to aim for more or less self-explanatory code. And by self-explanatory, I mean code that consistently follows my way of thinking or at least a consistent logic flow. In larger projects, debugging will become unnecessarily exhausting if you have to look for things for more than a couple of seconds.

but also more control without needing to code a bunch of methods.

More control and less complexity is a good argument for your solution. :slight_smile:

However, it is still impossible for me to form an opinion on it because I do not know if the advantages outweigh the disadvantages. The combination of “need to go looking for it” and “more work if its changing it” sounds like a potential case for refactoring/optimisation but the question is: Would it really be more work? Would you really have to look around?

If you always work this way, your solution would follow your way of thinking, not some random inspiration. You probably won’t have to look around if you want to change something. And unless you are part of a team, other people’s preferences do not really matter because you are the only one handling your code. Furthermore, each team has got its own rules, so it does not make much sense to please some imaginary team to improve your skills.

This is my favourite comic strip regarding this subject:

By the way, I checked your code again. Isn’t the PlayOneShotClip missing the second parameter in its definition? In your code, this line gets called: audioPlayer.PlayOneShotClip(shootingSFX, shootVolume);.

Yes, i set the default to always be the main camera since its 2d.

public void PlayOneShotClip(AudioClip audioclip, float volume)
    Vector3 cameraPos = Camera.main.transform.position;
    AudioSource.PlayClipAtPoint(audioclip, cameraPos, volume * clipMasterVolume);

And thank you Nina, much appreciate the discussion!
(And the comic was hilarious, wayyyy too accurate)

Have a wonderful day,

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