[SOLVED] Sound not playing in function called from another script

So I am implementing sound into my game and I am having an issue. I was able to get sound to play from keyboard inputs from my PlayerController script. Now I am trying to get a sound to play when an object gets destroyed.

I discovered trying to play a sound in some kind of OnTrigger event fails even if I call it before the object with the Audio Source is destroyed. So instead I created a SoundManager class that would have functions in it to play certain sounds.

Here is code inside of script attached to an asteroid:

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            soundManager.playExplosionFx();
            Destroy (other.gameObject);
            Destroy (gameObject);
            GameManager.lives--;
            if (GameManager.lives > 0)
            {
                gameManager.respawnPlayer();
            }
        }
    }

and here is my SoundManager class and the function I am calling in it:

using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour {


    public AudioClip explosionSound;
    private AudioSource source;
    private float volLowRange = .5f;
    private float volHighRange = 1.0f;


    void Awake ()
    {
        source = GetComponent<AudioSource>();
    }

    public void playExplosionFx()
    {
        float vol = Random.Range(volLowRange, volHighRange);
        source.PlayOneShot(explosionSound, vol);
        Debug.Log("explosion sound");
    }
}

No matter what I do the sound won’t play. Debugger logs correctly from both classes. The SoundManager class is attached to an empty game object and is set up properly with an Audio Source. Can you not play sounds from an empty game object? What am I missing?

Do you have a clip set. I don’t see where you’re setting the audio clip.
inside playExplosionFx()
source.clip = explosionSound

PlayOneShot takes the clip as a parameter, you only need to set the default clip on an audioSource if you want to use Play() or the others which use the default clip.

where is the empty in relation to the scene’s audio listener (default is on the camera)? you might be running into a situation where the source is too far from the listener so it’s faint/inaudible?

1 Like

It is in the center of my level. It’s just simple 2D and the level is no bigger than the size of the camera. I know this isn’t the issue because it picks up the sound when my player shoots and the empty game object is the same distance as the player (if not closer). For kicks, I even tried moving around the empty object but no luck. In case there was something wrong with my explosion sound effect I replaced it with the same sound effect I use for when the player shoots but not luck.

I haven’t touched any code since this post but an error just popped up while testing game. It says “The variable source of SoundManager has not been assigned.” It suggests maybe I need to assign it in inspector. Error points to line 21 of the SoundManager.

There is no need to assign it in inspector as the variable is private as you can see from my code. Not sure what is up with this error.

You might want to try setting the variable to public so you can make sure it’s getting assigned correctly on Awake(). Although I’m not sure why you wouldn’t have gotten that error sooner if that was the problem.

I did it but no luck. Error still pops up upon collision.

Can you post a screenshot of the inspector for the game object that has SoundManager and AudioSource components?

1 Like

Happily. Here is the inspector for my empty game object that has the SoundManager class attached to it as well as the AudioSource component:

Sorry one min having trouble uploading an image :confused:

Well, I’m not seeing any problems. It all looks like it should be working. One thing that could be causing problems is calling Destroy(gameObject) in the asteroid script with code after it, but it doesn’t seem likely to be the problem. What if the SoundManager script assigns the source in Start() instead of Awake()? It shouldn’t make a difference, but it’s worth a shot. Lastly, double check that the asteroid script has the correct reference to the SoundManager object. Failing that, I think I’m out of ideas…

1 Like

I gotta go to class right now but I will look into these things when I get back and let you know how it goes. I did try changing Awake to Start earlier out of a desperate attempt to fix things but it didn’t work. Thank you for all of your help I greatly appreciate it :slight_smile:

I figured it out. I apparently didn’t know the difference between prefabs in the project view and in the hierarchy. Source never got declared because my SoundManager prefab was never instantiated. I fixed it by changing the soundManager variable to private and declaring it like this in my asteroid class:

void Awake ()
    {
        soundManager = GameObject.FindObjectOfType<SoundManager>();
    }

Ah, glad you got it figured out!

1 Like