AudioSource.PlayOneShot Manual     Reference     Scripting  
Scripting > Runtime Classes > AudioSource
AudioSource.PlayOneShot

function PlayOneShot (clip : AudioClip, volumeScale : float = 1.0F) : void

Description

Plays an AudioClip.

Scales the AudioSource volume by volumeScale.

JavaScript
@script RequireComponent(AudioSource)
// Play impact audio clip when colliding with something
var impact : AudioClip;
function OnCollisionEnter () {
audio.PlayOneShot(impact);
}

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class example : MonoBehaviour {
public AudioClip impact;
void OnCollisionEnter() {
audio.PlayOneShot(impact);
}
}

import UnityEngine
import System.Collections

[RequireComponent(AudioSource)]
class example(MonoBehaviour):

public impact as AudioClip

def OnCollisionEnter():
audio.PlayOneShot(impact)