Audio Source (Scripting API)

Objects with an Audio Source component can play sounds through scripts.

Objects with an Audio Source component can play sounds through scripts. Audio playback is fully explicit, nothing auto-plays unless you tell it to.


Basic usage

Attach an Audio Source component to an object and assign an audio clip path.

From a script on that object:

if (ctx.HasAudioSource()) {
    ctx.SetAudioLoop(true);
    ctx.SetAudioVolume(0.85f);
    ctx.PlayAudio(); // start current clip
    ctx.StopAudio(); // stop playback (will not auto-restart)
    ctx.SetAudioClip("Assets/Audio/new.ogg"); // swap clip
    ctx.PlayAudio(); // start the new clip
}

Functions

HasAudioSource()

Checks whether the object has an Audio Source component.

Use this before calling any audio functions.


PlayAudio()

Starts playback of the currently assigned clip.

  1. Does nothing if no clip is assigned

  2. Does not auto-loop unless looping is enabled


StopAudio()

Stops audio playback.

  1. Audio will not automatically restart

  2. Playback resumes only if PlayAudio() is called again


SetAudioLoop(bool loop)

Enables or disables looping.

  1. true → audio loops

  2. false → audio plays once


SetAudioVolume(float volume)

Sets playback volume.

  1. Volume is clamped to 0.0 - 2.0

  2. 1.0 is normal volume


SetAudioClip(string path)

Assigns a new audio clip.

  1. Does not auto-play

  2. Call PlayAudio() to start playback

  3. Invalid paths fail safely (no crash)


Notes

Explicit behavior Audio will never auto-play or auto-restart unless explicitly instructed.

Timing Audio state changes apply on the next audio update tick.

Last updated

Was this helpful?