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. Dismiss Notice

Stopping audio clips

Discussion in 'Scripting' started by caseyboundless, Sep 6, 2014.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I have different buttons that play these sounds the problem is the sounds will play over each other if you click buttons really fast. I want to play a sound and if another audio is playing stop it so you only here the sound you played. Any idea? Booleans?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayCalls : MonoBehaviour {
    5.  
    6.     public AudioClip hailCall;
    7.     public AudioClip feedCall;
    8.     public AudioClip lonesomeHenCall;
    9.     public AudioClip greetingCall;
    10.     public AudioClip comebackCall;
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.    
    17.    
    18.    
    19.    
    20.    
    21.    
    22.    
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.    
    28.     }
    29.  
    30.  
    31.  
    32.     public void HailCall()
    33.     {
    34.  
    35.  
    36.             if (AudioClip(feedCall))
    37.         {
    38.             audio.Stop(feedCall);
    39.         }
    40.  
    41.  
    42.         audio.PlayOneShot (hailCall, 1.0f);
    43.  
    44.  
    45.     }
    46.  
    47.  
    48.     public void FeedCall()
    49.     {
    50.  
    51.  
    52.  
    53.  
    54.            
    55.            
    56.  
    57.  
    58.  
    59.  
    60.         audio.PlayOneShot (feedCall, 1.0f);
    61.        
    62.        
    63.     }
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Use audio.isPlaying to check whether a sound is currently playing. If so, stop it and play another one.
     
  3. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I tried it didn't work. I just tried this and it doesn't work either.
    Code (CSharp):
    1. public void HailCall()
    2.     {
    3.         audio.Stop ();
    4.         audio.Pause ();
    5.         audio.clip = hailCall;
    6.         audio.Play ();
    7.    
    8.     }
    9.  
    10.  
    11.     public void FeedCall()
    12.     {
    13.  
    14.  
    15.         audio.Stop ();
    16.         audio.Pause ();
    17.         audio.clip = feedCall;
    18.         audio.Play ();
    19.            
    20.  
    21.  
    22.  
    23.  
    24.    
    25.        
    26.        
    27.     }