Legacy Documentation: Version 5.1
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 function PlayOneShot(clip: AudioClip, volumeScale: float = 1.0F): void;

Parameters

clip The clip being played.
volumeScale The 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 audio: AudioSource;
function Start() {
	audio = GetComponent.<AudioSource>();
}
function OnCollisionEnter() {
	audio.PlayOneShot(impact, 0.7F);
}

See Also: AudioSource.Play.