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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Sound won't play when button pressed

Discussion in 'Scripting' started by doubleoseven, Apr 30, 2015.

  1. doubleoseven

    doubleoseven

    Joined:
    Sep 17, 2014
    Posts:
    10
    I'm trying to play a sound whenever I press a button. The button is supposed to check if a certain condition is met, and based on that, it plays a different sound. I'm using a SoundManger script in which the AudioSouce is initialised and it has a function called: PlaySingle. It's definition is as follows:

    Code (CSharp):
    1.  
    2.     public void PlaySingle(AudioClip clip)
    3.     {
    4.         soundEffect.clip = clip;
    5.         soundEffect.Play ();
    6.         Debug.Log ("Played!");
    7.     }
    8.    
    Every time the button is clicked, and if the condition is met, it plays the sound by the following line:

    Code (CSharp):
    1. musicPlayer._instance.PlaySingle(correct);
    where "correct" is an audio clip that is to be played.

    Unfortunately, each time I press the button, it displays the "Played" message on the console, but the sound isn't played. Does anyone have any idea why that may be the case?
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Where is the correct parameter being initialized? You say the AudioSource is inititliazed, but the AudioClips need to be initialized too. I can't really tell based on the code above, but it seems like correct is not initialized and therefore there is no clip to play.

    Do you have something like this?:

    Code (CSharp):
    1. [RequireComponent(typeof(AudioSource))]
    2. public class myClass : MonoBehaviour {
    3. public AudioClip correct;  
    4. public AudioSource soundEffect;  
    5. .
    6. .
    7. .
    8. //section1
    9. public void PlaySingle(AudioClip clip)
    10. {
    11.     soundEffect.clip = clip;
    12.     soundEffect.Play();
    13.     Debug.Log("Played!");
    14. }
    15.  
    16. //section2
    17. void someFunction()
    18. {
    19.    // other code
    20.     musicPlayer.instance.PlaySingle(correct);
    21. }
    22. // note: this is all one script on one object, but if section2 is in a different script, then you just have to pass the "correct" parameter as you are doing, but it needs to be initialized before passing
    23. }
    If I understand correctly, there are two scripts: one that contains section1 and another that contains section2. Section2 is an audio manager, and section1 is on an object that is supposed to play the sound. If that is the case, the audio manager should have the clips that are to be played and does not need an AudioSource; the other object is the one that needs the AudioSource. If you want the sound manager to play the sound, then it needs an AudioSource.



    Here is a basic script I wrote and it works for me:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class PlaySoundScript : MonoBehaviour {
    6.  
    7.  
    8.     public AudioClip myClip; // I drag a clip into the inspector to be used as the clip to be played
    9.     AudioSource myAudio; // I declare an AudioSource because the object that this script is attached to will play the sound
    10.     /
    11.     void Start ()
    12.    {
    13.         myAudio = GetComponent<AudioSource>(); // I initialize the AudioSource
    14.     }
    15.    
    16.  
    17.     void Update ()
    18.     {
    19.         if(Input.GetKeyDown(KeyCode.X)) { // I check a condition that I arbitrarily chose
    20.             myAudio.clip = myClip; // I set the AudioSource's clip to be the clip I dragged in the inspector
    21.             myAudio.Play(); // I play the clip
    22.         }
    23.    
    24.     } //end of Update
    25. } // end of class
    26.  


    I hope this helps. If not, can you paste the whole script(s)?
     
    doubleoseven likes this.
  3. doubleoseven

    doubleoseven

    Joined:
    Sep 17, 2014
    Posts:
    10
    I did as you suggested: I declared and initialised both the AudioSource and AudioClip(s) in a single script and wrote a function that plays the AudioClip using the AudioSource. When I called it from another script, it worked.

    Thank you so much for your help!