Audio doesn't work

Hello guys, I’m very new to Unity, been doing the Scavengers tutorial (2D Roguelike) from the learning section.
I’ve reached the 13 out of 14 video, just adding the sound
I’m doing exactly as they do it in there, but it does not work.
I’ve been typing the exact same code, and did the exact same SoundManager game object, and it just doesn’t work.
I’m currently half way of the video tutorial to add the sound, but the background music should be on by now.

here is the code of my SoundManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManager : MonoBehaviour {

    public AudioSource efxSource;
    public AudioSource musicSource;
    public static SoundManager instance = null;

    public float lowPitchRange = .95f;
    public float highPitchRange = 1.05f;

    void Awake ()
    {
        if (instance = null)
            instance = this;
        else if (instance != this)
            Destroy(gameObject);

        DontDestroyOnLoad(gameObject);
    }

    public void PlaySingle(AudioClip clip)
    {
        efxSource.clip = clip;
        efxSource.Play();
    }
   
    public void RandomizeSfx(params AudioClip [] clips)
    {
        int randomIndex = Random.Range(0, clips.Length);
        float randomPitch = Random.Range(lowPitchRange, highPitchRange);

        efxSource.pitch = randomPitch;
        efxSource.clip = clips[randomIndex];
        efxSource.Play();
    }

}

and a screen shot

Can you show the code that calls either PlaySingle or RandomizeSfx?

Don’t have that.
I’m at the same stage as the tutorial, and when he launch the game, the sound works.
the SoundManager is the only new thing that I added
but if in the tutorial video it works for him, then it should work for me too.

I’ve put the Audio Source in the SoundManager, and checked the “Loop” same as he did.
here is the tutorial video, im at 6:13, this is where he launches the game, and the background music works

What you have so far should work… you’ve got your sound manager in the scene… your sound manager has an audio source on it with a clip attached… it’s set to play on awake. Weird.

Open up the audio right in Unity and play it. You can click on the audio file in Unity’s project explorer, and there should be a play button or something in the preview. Make sure you’re hearing sounds. Maybe your PC’s sound isn’t working right…??

I did open the audio file, and it does work.
But in game, nothing.
This makes me frustrated, I don’t want to keep going until I got it all working

Does your Camera have an AudioListener component attached to it? Did you somehow end up with multiple SoundManager script instances in the scene?

1 Like

How can I check if I have multiple SoundManager script instances?

And yes, my Camera does have AudioListener component attached

Add a log statement in your Start method or search for “t:SoundManager” in the scene hierarchy.

Only one in the hierarchy

Since you are on windows, you should have a sound icon in your system tray (I see it in your screenshot, so it’s there). When you play the game, click on that icon and see if sound is playing there. If you don’t see anything there, check the mixer to make sure the volume on the Unity player isn’t muted/turned down.

It was the first thing I did.
Unity volume is on max.

Is your SoundManager gameobject inactive?

How can I check that?
the V in the SoundManager is checked (in the inspector)

No, it is, I see it on in the one screenshot now. Well, you could zip up your project and provide it here.

Other thing to try is to try to play the audio through the script to see if that even works.

I also thought it could be your sound was set up as 3d, but if you’re following the tutorial, I don’t think that would be it.

Here, that’s my project.
https://ufile.io/k293v

I can look at it later when I’m home, unless someone else gets a chance to.

Thank you.
Much appreciated.

if(instance = null)

you have the above in your code but it should read

if (instance == null)

a double == means is it equal, not it equals, might help along the way

It looks to me that your instance of the soundmanager class is never being set, so it just keeps getting nullified.

2 Likes

Good catch! Surprised how many of us looked at this and didn’t notice that. And fixing that will fix your sound playing issue.