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

[C#] Play sound?

Discussion in 'Scripting' started by Kovenant, Jan 19, 2015.

  1. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Hi all.. I think I'm going senile.. Nothing works for me anymore it seems.. :/
    I'm trying to play a sound of a door opening, using Animation Events, but the sound won't play. What am I doing wrong here?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayDoorSound : MonoBehaviour {
    5.  
    6.     public AudioClip doorOpenSound;
    7.     public AudioClip doorCloseSound;
    8.  
    9.     public void PlayDoorOpenSound()
    10.     {
    11.         GetComponent<AudioSource>().PlayOneShot(doorOpenSound, 0.7f);
    12.         Debug.Log("Door Sound Played!");
    13.     }
    14. }
    I'm calling the void "PlayDoorOpenSound()" with Animation Events as said above, but something's wrong with the code I guess?
     
  2. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    Does it print "Door Sound Played!" in the debug console?

    How is your audio source set up? is it 2d or 3d. If 3D, then are you close enough to hear it in your audio listener?
     
  3. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Nope, nothing in the Console.. :/ It is a 3D sound.. It would be strange if I wasn't close enough to hear it (it's a door....) ;)
     
  4. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    If it dosen't debug you the message in the console, you have no acces to that function.
    Means you have to set a Trigger to to Enter the Sound... i gave you 2 Examples. (see below)

    For this Example you need on the Door a Triggerd Collider and one of this 2 Objects must have a Rigidbody Component, i would attach that to the player ;)
    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2. {
    3. audio.PlayOneShot(audioFile);
    4. }
    The other way to do this, and it's a bit faster then Triggerd Colliders, is to check the Distance between the Player and the Door and if the Player is in a range to that door you can Trigger a AudioClip.
    Code (CSharp):
    1. float inRangeRadius = 10f;
    2. bool allowToPlay = false;
    3.  
    4. void Update ()
    5. {
    6. float distance = Vector3.Distance (transform.position, door.transform.position);
    7.  
    8. if(distance <= inRangeRadius)
    9. {
    10. if(allowToPlay)
    11. {
    12. allowToPlay = false;
    13. audio.PlayOneShot(audioFile);
    14. }
    15. }
    16. else
    17. {
    18. allowToPlay = true;
    19. }
    20. }
     
  5. ItsaMeTuni

    ItsaMeTuni

    Joined:
    Jan 19, 2015
    Posts:
    44
    Im sorry, this could be a dumb reply, if it is, sorry.
    But....
    Did u call your method? The PlayDoorOpenSound().
     
  6. Kovenant

    Kovenant

    Joined:
    Sep 18, 2013
    Posts:
    254
    Yes, that function is called using Animation Events..:)