Defining AudioSource parameters in code
Pretty much everything in the AudioSource can be easily defined in code. Here’s what the AudioSource looks like:
Let’s go down the list...
private AudioSource source; private AudioClip clip; private AudioMixerGroup outputMixerGroup; private bool isLoop; private bool playOnAwake; private bool mute; private float volume = 1f; private float pitch = 1f; source.clip = clip; //defines the Audio Clip which is assigned to the AudioSource.
source.outputMixerGroup = outputMixerGroup; //defines the output group (Unity 5 onwards)
source.loop = isLoop; //defines whether it’s looping
source.playOnAwake = playOnAwake; //defines whether it plays on awake
source.mute = mute; //defines whether it’s muted
source.volume = volume; //defines volume. Above, I’ve assigned 1f as the default.
source.pitch = pitch; //defines pitch. Above, I’ve assigned 1f as the default. OR
source.pitch = Random.Range (0.8f, 1.2f); //this assigns a random range between 0.8 and 1.2, so everytime you trigger this, it will return a random pitch within this range.
Many others can be accessed by code. For the full list, click here!
Notice that on the AudioSource, those which are checkboxes are defined by “bool”, short for “boolean”; there are only 2 options for booleans - true or false.
Those with sliders/number ranges are “floats”, which are numbers with decimal places. Always put an “f” behind the number! You can also use “int” for integer, but these are limited to whole numbers.









