Sound Not Playing - Unity 5.6

Hi,

I’m playing around with the Roll a Ball Tutorial and have started adding my own things to it.

I have a Main Menu with an audio listener attached to the camera. From this screen I can access “Test Level”.

Test Level contains a Ball(Tag = untagged), a Pickup(Tag = Pickup10) and a PickupTrigger(Tag = PickupTrigger) surrounding the Pickup. The aim being that when the ball rolls into the cube:
A particle effect appears
A sound effect plays
the cube and trigger disappear 1/2 a second later.
(the 1/2 second is to allow the effect to play)

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

public class PickupEffect : MonoBehaviour {

    public ParticleSystem effect;
    public GameObject trigger;
    public AudioSource pickupSound;

    void OnTriggerEnter (Collider other)
    {
        if (GetComponent<Collider> ().gameObject.CompareTag("PickupTrigger"))
        {
            effect.Play();
            pickupSound.Play();
            StartCoroutine(StopTrigger());
            }
    }

    IEnumerator StopTrigger()
    {
        yield return new WaitForSeconds(0.5f);

        trigger.SetActive(false);
    }
}

The correct Particle System is placed into “effect”
The trigger is placed into “trigger”
the sound effect is placed into “AudioSource”

The ball rolls into the pickup trigger, the particle effect plays and the pickup disappears. However, no sound plays.

I have a similar scene setup with what seems to be the same code and that works fine, but if I import the code into this project and set it up the same, no sound plays.

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

public class Trigger : MonoBehaviour {

    public ParticleSystem effect;

    public GameObject trigger;
    public AudioSource pickupSound;

    void OnTriggerEnter(Collider other)
    {
        if (GetComponent<Collider>().gameObject.CompareTag("PickupTrigger"))
        {
            effect.Play();
            pickupSound.Play();
            StartCoroutine(StopTrigger());

        }
    }

    IEnumerator StopTrigger()
    {
        yield return new WaitForSeconds(0.5f);

        trigger.SetActive(false);
    }

}

I’m at a loss as to what is going wrong, can someone help please?

Any chance the clip is missing in the audio source, or the volume is too low/off?
Sounds like your setup is accurate, otherwise - not sure what is wrong.

However, on an unrelated note, the conditional check inside your OnTriggerEnter is odd.
Is this script on the trigger itself, right? I think you might want to be doing:

if (other.CompareTag("Ball")) { // I know your ball is untagged, but if it were tagged, then this check would mean only the ball (player) could trigger this code. Right now, your code is saying ".. On trigger, get my own collider, and then compare its tag (my own tag) to see if it's.. my tag.. if so, continue." :)
// your code here
 }

note: there is a long comment inside the code portion! :slight_smile:

Edit: Btw, I think it’s cool that you’re expanding on the tutorial :wink:

Yeah I’m not sure how I got to where I did with the conditional statement. Thank you for your help, I will double check it when I get back from work

no problem. That wasn’t breaking it. still not sure why your sound doesn’t work, but I hope you get it fixed. :slight_smile:

My apologies, I’ve only just managed to get to the project again lol.
It was the volume on the sound being too low.

Thank you for your help and kind comments

Cool, glad you got it resolved :slight_smile: