Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by bekiryanik · Jul 07, 2014 at 07:29 PM · soundsounds

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bekiryanik · Jul 07, 2014 at 08:51 PM 0
Share

Thank you. I am going to work on your codes. They are seemed well to me.

avatar image bekiryanik · Jul 07, 2014 at 08:53 PM 0
Share

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?

1234.png (12.4 kB)
avatar image HenryStrattonFW · Jul 07, 2014 at 09:01 PM 0
Share

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.

avatar image bekiryanik · Jul 07, 2014 at 11:44 PM 0
Share

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

}

avatar image HenryStrattonFW · Jul 08, 2014 at 09:21 AM 0
Share

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.

Show more comments
avatar image
3

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!


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges