Search Unity

Audio Can't seem to time audio to play?

Discussion in 'Audio & Video' started by f1gment, Feb 25, 2019.

  1. f1gment

    f1gment

    Joined:
    Nov 22, 2018
    Posts:
    16
    I have the following code, but the button click sound doesn't play when the button is pressed. I can set the wait time to be a million seconds, and it doesn't work.

    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         source = GetComponent<AudioSource>();
    4.     }
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         screen.SetActive(false);
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.        
    14.     }
    15.  
    16.     public void OnExit()
    17.     {
    18.         screen.SetActive(true);
    19.     }
    20.  
    21.     public void OnNoPress()
    22.     {
    23.         source.PlayOneShot(button);
    24.         StartCoroutine(Wait());
    25.         screen.SetActive(false);
    26.     }
    27.  
    28.     public void OnYesPress()
    29.     {
    30.         source.PlayOneShot(button);
    31.         StartCoroutine(Wait());
    32.         Application.Quit();
    33.     }
    34.  
    35.     IEnumerator Wait()
    36.     {
    37.         yield return new WaitForSeconds(0.25f);
    38.     }
    39. }