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.

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();

:slight_smile:

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)"));
	}
}