- Home /
How to use audio commands and Audio Source?
Hi
I have some questions about sound in Unity.
1: How do I use a Audio Source? Can I put a empty Audio Source on an object and then put use a scrpit to control sounds? I play different AudioClip variables.
2: What more functions regarding sound are there than PlayOneShot? I have tried Play and Stop, but it does not work as planned.
Answer by AlucardJay · Jan 26, 2012 at 07:08 PM
Hey, I am new to unity and not much of a programmer, but I have the same problem. I think I can answer one part.
I created an Empty gameObject, gave it the component Audio Source (untick play on awake, and leave the audio clip box at None / empty). Then I applied this script. (Don't forget to add the sounds to the prefab / object in the inspector after you have attached the script and component audio source).
// random sounds script
// ----
// Inspector Variables
// Sounds
var scream1 : AudioClip;
var scream2 : AudioClip;
var scream3 : AudioClip;
var screamSound : AudioClip;
var randomSound : int = 0;
// ----
// Game Loop
function FixedUpdate ()
{
// when you want to play a sound (one shot) , call the function
// playSound ();
}
// ----
//
function playSound ()
{
randomSound = Random.Range(0,10);
if ((randomSound >= 0) && (randomSound <= 3)) {screamSound = scream1;}
if ((randomSound > 3) && (randomSound <= 6)) {screamSound = scream2;}
if ((randomSound > 6) && (randomSound <= 10)) {screamSound = scream3;}
audio.PlayOneShot(screamSound);
}
// ----
for pausing or playing a sound, I have exactly the same problem, but these commands work when you attach the component audio source and place the sound in the audio clip box :
audio.volume = 0.0; audio.mute = true;
audio.volume = 1.0; audio.mute = false;
apart from that , I hope you get an answer so we both find out how to play / pause audioclip audio.Play();
:)
@alucardj, you could do it more easier with a switch statement:
function playSound () { randomSound = Random.Range(0,3); switch (randomSound){ case 0: screamSound = scream1; break; case 1: screamSound = scream2; break; case 2: screamSound = scream3; break; } audio.PlayOneShot(screamSound); }Or even easier yet using an array:
var screams: AudioClip[]; // set size and fill it in the Inspector
function playSound () {
audio.PlayOneShot(screams[Random.Range(0, screams.length)]); }
ahh, thanku very much. I came to unity from flash, and one command i was missing the most was switch - case. But the array method is the most elegant. (my bad random range scale was a leftover from before i realized Random.Range(0.0, 2.0) gave float , rr(0, 2) gave int. I should've fixed that too) $$anonymous$$any thanks.
Answer by sjurick · Feb 28, 2014 at 03:53 PM
Help please. I have implemented the array solution as exampled above. I have an audio listener attached to the same prefab that the audio script is attached too with the array. I have two audio clips assigned to the array via the inspector. The only time audio works is when I actually assign an audio clip to the audio source, then, that's the only audio that gets played, not a random mix of the two.
using UnityEngine;
using System.Collections;
public class Target : MonoBehaviour {
public AudioClip[] audioClips;
void Start () { }
void Update () { }
public void TargetHit ()
{
Debug.Log("Made it to Target.cs script");
audio.PlayOneShot(audioClips[Random.Range(0,audioClips.Length)]);
Destroy(this.gameObject);
//Destroy(GameObject.Find("Target(Clone)"));
}
}