Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sound Not Playing - Unity 5.6

Discussion in 'Scripting' started by thegreatkoala, Dec 17, 2017.

  1. thegreatkoala

    thegreatkoala

    Joined:
    Jun 30, 2017
    Posts:
    11
    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)

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PickupEffect : MonoBehaviour {
    7.  
    8.     public ParticleSystem effect;
    9.     public GameObject trigger;
    10.     public AudioSource pickupSound;
    11.  
    12.     void OnTriggerEnter (Collider other)
    13.     {
    14.         if (GetComponent<Collider> ().gameObject.CompareTag("PickupTrigger"))
    15.         {
    16.             effect.Play();
    17.             pickupSound.Play();
    18.             StartCoroutine(StopTrigger());
    19.             }
    20.     }
    21.  
    22.     IEnumerator StopTrigger()
    23.     {
    24.         yield return new WaitForSeconds(0.5f);
    25.  
    26.         trigger.SetActive(false);
    27.     }
    28. }
    29.  
    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.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Trigger : MonoBehaviour {
    7.  
    8.     public ParticleSystem effect;
    9.  
    10.     public GameObject trigger;
    11.     public AudioSource pickupSound;
    12.  
    13.     void OnTriggerEnter(Collider other)
    14.     {
    15.         if (GetComponent<Collider>().gameObject.CompareTag("PickupTrigger"))
    16.         {
    17.             effect.Play();
    18.             pickupSound.Play();
    19.             StartCoroutine(StopTrigger());
    20.  
    21.         }
    22.     }
    23.  
    24.     IEnumerator StopTrigger()
    25.     {
    26.         yield return new WaitForSeconds(0.5f);
    27.  
    28.         trigger.SetActive(false);
    29.     }
    30.  
    31. }
    32.  
    I'm at a loss as to what is going wrong, can someone help please?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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:
    Code (csharp):
    1. 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." :)
    2. // your code here
    3.  }
    4.  
    note: there is a long comment inside the code portion! :)

    Edit: Btw, I think it's cool that you're expanding on the tutorial ;)
     
    Last edited: Dec 17, 2017
  3. thegreatkoala

    thegreatkoala

    Joined:
    Jun 30, 2017
    Posts:
    11
    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
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    no problem. That wasn't breaking it. still not sure why your sound doesn't work, but I hope you get it fixed. :)
     
  5. thegreatkoala

    thegreatkoala

    Joined:
    Jun 30, 2017
    Posts:
    11
    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
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you got it resolved :)