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 /
avatar image
0
Question by nickostan · Jun 04, 2020 at 11:25 PM · audiosoundaudiosourcesounds

Play many sounds (60 to 200) without Unity cutting sound

Hello,

I am building a game whereby many enemies on the screen die in quick succession. This can be upwards of 50 to 200 enemies all dying at once. When an enemy dies, it spawns a GameObject (from a pool) that has an AudioSource and plays the enemy's death sound.

Once the game hits ~60 deaths, the sound completely cuts out for about 2 seconds. This is super irritating and I have to fix it ASAP. Obviously, it is inefficient to play this many sounds at once so I need a solution, or a work around, to play a fraction of the sounds without the 'sound buffer' overloading.

I.e. is there a way to tell how many sounds are playing, and restrict any further sounds from playing? Or setting a threshold between which sounds of the same type can be played?

Thanks so much!

Comment
Add comment · Show 1
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 nickostan · Jun 08, 2020 at 05:17 PM 0
Share

bumping :-(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jun 08, 2020 at 06:13 PM

This seems like a scenario where you could make good use of Update() vs. LateUpdate() timing.

You can keep a global controller around to listen for enemies to report their deaths, then play pre-mixed sounds accordingly based on the quantity.

First, an example script for the controller to listen for enemies dying:

 public class DeathWatch : MonoBehaviour
 {
     // Sets up a singleton that can be called via a static variable
     // It will create itself when requested if it doesn't already exist
     // And will assign itself if it already does
     private static DeathWatch sdInstance;
     public static DeathWatch instance
     {
         get
         {
             if(sdInstance == null)
             {
                 // Expensive, but only has to be performed only once
                 DeathWatch verify = FindObjectOfType<Deathwatch>();
                 if(verify == null)
                 {
                     GameObject newObj = new GameObject();
                     sdInstance = newObj.AddComponent<T>();
                 }
                 else
                 {
                     sdInstance = verify;
                 }
             }
             return sdInstance;
         }
     }

     void Awake()
     {
         sdInstance = this;
     deathReported = false;
         currentDeathCount = new Dictionary<string, int>();
     }

     // DeathWatch-specific from here down
     bool deathReported; // improves efficiency
     Dictionary<string, int> currentDeathCount;
     
     public void ReportDeath(string enemyType)
     {
         if(currentDeathCount.ContainsKey(enemyType))
         {
             currentDeathCount[enemyType]++;
         }
         else
         {
             currentDeathCount[enemyType] = 1;
         }
         deathReported = true;
     }
     
     void LateUpdate()
     {
         if(deathReported)
         {
             foreach(KeyValuePair<string, int> kvp in currentDeathCount)
             {
                 // Play sound based on the enemy type kvp.Key
                 // and death count kvp.Value
             }
             currentDeathCount.Clear();
             deathReported = false;
         }
     }
 }


Next, we have the simple adjustment to the enemy script to support this:

 void Update()
 {
     // Example death
     if(health <= 0)
     {
         Die();
     }
 }

 void Die()
 {
     DeathWatch.instance.ReportDeath(enemyTypeIdentifier);
     PlayDeathSequence();
 }

... Yep, it's basically two lines for the enemy units. One to provide an identifier (a string is just an example. You could use any type of value to identify them and adjust the Dictionary accordingly) and one to report their death for the current frame.

That said, if you intend to batch them within a small time window rather than same-frame only, you'll need a little bit more information (for instance, the integer portion of the Dictionary could be replaced with a class/struct for more detailed and grouped information as desired).

Edit: (Whoops, messed up a copy/paste and forgot to change a variable name)
Edit 2: (Fixed another simple script error)

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

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio not coming out of speakers 1 Answer

AUDIO ISSUES - PlayOneShot... is cutting short... I think? 2 Answers

Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers

How to start a sound not from it's start ? 2 Answers

Sound Clip isn't playing when triggered. 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