- Home /
One of the two sound stop while other one is playing.
Hello,
I have sound problem in my game. There are three sounds on one game objects. And sometimes, while playing the game, the game object needs to use two sound in same time, but it can't use them in same time. Unity stops one of them and let the other one plays. And the other one should also keep playing when necessary in that time either but it is stopped by unity. What should i do to manage these sounds? Here is example codes of sounds that i used.
public AudioClip[] audioClip;
void PlaySound(int clip)
{
audio.clip = audioClip[clip];
audio.Play ();
}
void Die()
{
PlaySound(0);
isDead = true;
}
}
Answer by HenryStrattonFW · Jul 07, 2014 at 07:59 PM
This is because with unity an AudioSource component only deals with a single AudioClip at a time. You can get around this in a few ways, one way is to attach multiple AudioSource components, one for each clip. This method does mean however that you cannot use the audio property of a MonoBehaviour(since that will only get you the first). Instead you can either use a GetComponents() on your Start and store the resulting array. Or you can add the AudioSource components at runtime (one for each clip) in your Start function for example, again store an array of the AudioSources so that you can easily know which source corresponds to which clip. Here is an example script (NOTE: untested but should be ok)
using UnityEngine;
public class MyClass : MonoBehaviour
{
public AudioClip[] audioClips;
public bool isDead { get; private set; }
private AudioSource[] _sources;
void Awake()
{
_sources = new AudioSource[audioClips.Length];
for (int i = 0; i < audioClips.Length; i++)
{
_sources[i] = gameObject.AddComponent<AudioSource>();
_sources[i].clip = audioClips[i];
// set up the properties such as distance for 3d sounds here if you need to.
}
}
public void PlaySound(int clipIdx)
{
if (clipIdx >= 0 && clipIdx < _sources.Length)
_sources[clipIdx].Play();
}
private void Die()
{
PlaySound(0);
isDead = true;
}
}
Thank you. I am going to work on your codes. They are seemed well to me.
The first way didn't work for me. I am going to try second way but i didn't understand it well cuz of i am new at unity. :) I understood that, i need to change $$anonymous$$onoBehaviour code and i need to use GetComponents code at the beginning. Then after, what do i need to do if i use these codes? The same codes above?
http://answers.unity3d.com/storage/temp/28822-1234.png
As you can see in picture, i can set the audio number. I guess i need to change this way but how?
Well you would just need to copy parts of my code into yours, obviously if you already have an Awake function you don't want to duplicate it all, just copy over the contents of the function. Also I called my variable audioClips for clarity, ins$$anonymous$$d of audioClip, so keep in $$anonymous$$d you will have to rename that in my code to match yours.
Also note that the code I provided is only for the second approach (creating the audiosources at runtime) So you do not need to do the GetComponents() as you will be creating them on the fly.
I did but nothing changed. I'll show you my codes when i use your codes.
using UnityEngine;
using System.Collections;
public class Bird : $$anonymous$$onoBehaviour {
public float tapForce; // Value that controls the amount of force applied when clicking
public $$anonymous$$ap map; // A reference to the map class
public Game$$anonymous$$anager manager; // Reference to the manager class
private bool isDead; // Bool to check if we've died or not
public AudioClip[] audioClips;
public bool isDead { get; private set; }
private AudioSource[] _sources;
//public AudioClip[] audioClip;
void Update ()
{ // Check to see if we're clicking and if we've not already died
if (Input.Get$$anonymous$$ouseButtonDown(0) && !isDead && !(Camera.main.WorldToViewportPoint(transform.position).y > 1f))
{
// Add our tapForce to our bird's velocity if we do click
rigidbody2D.velocity = new Vector2(0f, tapForce);
// Control the rotation of the bird based on its velocity
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0f,0f,0f), Quaternion.Euler(0f,0f,90f), rigidbody2D.velocity.y);
PlaySound(1);
} else if (rigidbody2D.velocity.y < -.05)
{
// Do the same here except only if it is falling
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0f,0f,0f), Quaternion.Euler(0f,0f, -90f), -rigidbody2D.velocity.y * 4f);
}
}
//void PlaySound(int clip){
//audio.clip = audioClip[clip];
//audio.Play (); }
// Check to see if we hit ANYTHING that's not a trigger
void OnCollisionEnter2D(Collision2D other)
{
// If we have, call the bird's die method
Die ();
}
// If we hit a trigger, we know it is a gate trigger
void OnTriggerEnter2D(Collider2D other)
{
PlaySound (2);
// In that case, add one to our player's score
manager.curScore += 1;
map.Generate();
other.collider2D.enabled = false;
}
// $$anonymous$$ethod that controls the bird's death
void Awake()
{
_sources = new AudioSource[audioClips.Length];
for (int i = 0; i < audioClips.Length; i++)
{
_sources[i] = gameObject.AddComponent<AudioSource>();
_sources[i].clip = audioClips[i];
// set up the properties such as distance for 3d sounds here if you need to.
}
}
public void PlaySound(int clipIdx)
{
if (clipIdx >= 0 && clipIdx < _sources.Length)
_sources[clipIdx].Play();
}
private void Die()
{
PlaySound(0);
// Set a boolean of isDead to true so that we can do some checks later
isDead = true;
// $$anonymous$$ake sure we stop the map from moving
map.rigidbody2D.velocity = Vector2.zero;
// Force the Game Over window to pop up and generate our highscore
manager.showGameOver = true;
}
}
}
Since I cant see your whole project to test I'm not sure what's causing it, I'll try to put together a small example project at lunch for you.
Answer by Shkeek · Apr 01, 2016 at 02:47 AM
I've had success with using the PlayOneShot() method of the AudioSource.
Sample Code:
private AudioSource audio;
public AudioClip firstClip;
public AudioClip secondClip;
// in the Inspector window make sure to add each corresponding clip, and obviously have 1 AudioSource component
void Start() {
audio = GetComponent<AudioSource>();
}
void Update() {
if (somethingHappens) {
audio.PlayOneShot(firstClip);
}
if (somethingElseHappens) {
audio.PlayOneShot(secondClip);
}
}
//Hope this helps!
Answer by Khoray · Feb 17, 2018 at 10:11 PM
Try set the 'Priority' value to 0 from the preferences of AudioSource, or lower value then another audiosource which is less important.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to make multiple sound augmented reality in 1 scene UNITY? 0 Answers
Can't play multiple sounds at once 2 Answers
Sound only plays once 1 Answer
Audio not coming out of speakers 1 Answer
how to start a song in the middle? 1 Answer