Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AudioSource.PlayOneShot

Switch to Manual
public method PlayOneShot(clip: AudioClip, volumeScale: float = 1.0F): void;

Parameters

clipThe clip being played.
volumeScaleThe scale of the volume (0-1).

Description

Plays an AudioClip, and scales the AudioSource volume by volumeScale.

#pragma strict
@RequireComponent(AudioSource)
public var impact: AudioClip;
var audioSource: AudioSource;
function Start() {
	audioSource = GetComponent.<AudioSource>();
}
function OnCollisionEnter() {
	audioSource.PlayOneShot(impact, 0.7F);
}

See Also: AudioSource.Play.

Did you find this page useful? Please give it a rating: