Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

AudioSource

Switch to Manual

Namespace: UnityEngine

/

Inherits from: Behaviour

Description

A representation of audio sources in 3D.

An AudioSource is attached to a GameObject for playing back sounds in a 3D environment. In order to play 3D sounds you also need to have a AudioListener. The audio listener is normally attached to the camera you want to use. Whether sounds are played in 3D or 2D is determined by AudioImporter settings.

You can play a single audio clip using Play, Pause and Stop. You can also adjust its volume while playing using the volume property, or seek using time. Multiple sounds can be played on one AudioSource using PlayOneShot. You can play a clip at a static position in 3D space using PlayClipAtPoint.

See Also: AudioListener, AudioClip, AudioSource component.

Variables

bypassEffectsBypass effects (Applied from filter components or global listener filters).
clipThe default AudioClip to play.
dopplerLevelSets the Doppler scale for this AudioSource.
ignoreListenerPauseAllows AudioSource to play even though AudioListener.pause is set to true. This is useful for the menu element sounds or background music in pause menus.
ignoreListenerVolumeThis makes the audio source not take into account the volume of the audio listener.
isPlayingIs the clip playing right now (Read Only)?
loopIs the audio clip looping?
maxDistance(Logarithmic rolloff) MaxDistance is the distance a sound stops attenuating at.
minDistanceWithin the Min distance the AudioSource will cease to grow louder in volume.
muteUn- / Mutes the AudioSource. Mute sets the volume=0, Un-Mute restore the original volume.
panSets a channels pan position linearly. Only works for 2D clips.
panLevelSets how much the 3d engine has an effect on the channel.
pitchThe pitch of the audio source.
playOnAwakeIf set to true, the audio source will automatically start playing on awake.
prioritySets the priority of the AudioSource.
rolloffModeSets/Gets how the AudioSource attenuates over distance.
spreadSets the spread angle a 3d stereo or multichannel sound in speaker space.
timePlayback position in seconds.
timeSamplesPlayback position in PCM samples.
velocityUpdateModeWhether the Audio Source should be updated in the fixed or dynamic update.
volumeThe volume of the audio source (0.0 to 1.0).

Functions

GetOutputDataReturns a block of the currently playing source's output data.
GetSpectrumDataReturns a block of the currently playing source's spectrum data.
PausePauses playing the clip.
PlayPlays the clip with an optional certain delay.
PlayDelayedPlays the clip with a delay specified in seconds. Users are advised to use this function instead of the old Play(delay) function that took a delay specified in samples relative to a reference rate of 44.1 kHz as an argument.
PlayOneShotPlays an AudioClip, and scales the AudioSource volume by volumeScale.
PlayScheduledPlays the clip at a specific time on the absolute time-line that AudioSettings.dspTime reads from.
SetScheduledEndTimeChanges the time at which a sound that has already been scheduled to play will end. Notice that depending on the timing not all rescheduling requests can be fulfilled.
SetScheduledStartTimeChanges the time at which a sound that has already been scheduled to play will start.
StopStops playing the clip.

Static Functions

PlayClipAtPointPlays an AudioClip at a given position in world space.

Inherited members

Variables

enabledEnabled Behaviours are Updated, disabled Behaviours are not.
animationThe Animation attached to this GameObject (null if there is none attached).
audioThe AudioSource attached to this GameObject (null if there is none attached).
cameraThe Camera attached to this GameObject (null if there is none attached).
colliderThe Collider attached to this GameObject (null if there is none attached).
collider2DThe Collider2D component attached to the object.
constantForceThe ConstantForce attached to this GameObject (null if there is none attached).
gameObjectThe game object this component is attached to. A component is always attached to a game object.
guiTextThe GUIText attached to this GameObject (null if there is none attached).
guiTextureThe GUITexture attached to this GameObject (Read Only). (null if there is none attached).
hingeJointThe HingeJoint attached to this GameObject (null if there is none attached).
lightThe Light attached to this GameObject (null if there is none attached).
networkViewThe NetworkView attached to this GameObject (Read Only). (null if there is none attached).
particleEmitterThe ParticleEmitter attached to this GameObject (null if there is none attached).
particleSystemThe ParticleSystem attached to this GameObject (null if there is none attached).
rendererThe Renderer attached to this GameObject (null if there is none attached).
rigidbodyThe Rigidbody attached to this GameObject (null if there is none attached).
rigidbody2DThe Rigidbody2D that is attached to the Component's GameObject.
tagThe tag of this game object.
transformThe Transform attached to this GameObject (null if there is none attached).
hideFlagsShould the object be hidden, saved with the scene or modifiable by the user?
nameThe name of the object.

Functions

BroadcastMessageCalls the method named methodName on every MonoBehaviour in this game object or any of its children.
CompareTagIs this game object tagged with /tag/?
GetComponentReturns the component of Type type if the game object has one attached, null if it doesn't.
GetComponentInChildrenReturns the component of Type type in the GameObject or any of its children using depth first search.
GetComponentInParentReturns the component of Type type in the GameObject or any of its parents.
GetComponentsReturns all components of Type type in the GameObject.
GetComponentsInChildrenReturns all components of Type type in the GameObject or any of its children.
GetComponentsInParentReturns all components of Type type in the GameObject or any of its parents.
SendMessageCalls the method named methodName on every MonoBehaviour in this game object.
SendMessageUpwardsCalls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.
GetInstanceIDReturns the instance id of the object.
ToStringReturns the name of the game object.

Static Functions

DestroyRemoves a gameobject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadMakes the object target not be destroyed automatically when loading a new scene.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsOfTypeReturns a list of all active loaded objects of Type type.
InstantiateClones the object original and returns the clone.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares if two objects refer to the same.