Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 knightgeez · May 19, 2021 at 08:05 PM · audioaudiosourceaudioclipmultiple

How can I play multiple audioclips from the same object?

Hi! Beginner Unity solo student here. I am only starting to understand the C# programming and void stuff, etc. I have a problem that I have made a rocket boost game; when I press Spacebar, the thrust engine sound starts playing. As soon as It's not pressed anymore, thrust engine sound stops through:

 private void StopThrusting()
     {
         audioSource.Stop();
         mainEngineParticles.Stop();
     }

However, I would also like to have a Sound effect when rocket hits an item with class "Fuel", I thought using:

 AudioSource audioSource;
  AudioClip FuelSoundEffect;
 public void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.CompareTag("Fuel")){
               
             CurrentFuel += BurningRate;
           FuelSound();
             other.gameObject.GetComponent<MeshRenderer>().enabled = false;
             other.gameObject.GetComponent<Light>().enabled = false;
         }
 
     }
 
 public void FuelSound()
 {
 audioSource.PlayOneShot(FuelSoundEffect);
 }

However the problem here, is that StopThrusting ends the audioSource by calling audioSource.Stop(); when I release Spacebar.

How is it possible to stop only the mainEngine sound effect, but not the FuelSound effect when colliding with "Fuel" item?

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

2 Replies

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

Answer by rh_galaxy · May 20, 2021 at 04:39 AM

You can have more than one AudioSource... I have one for each constant/looping sound and one general for all of the one shot sounds since they can play simultaneously.

So in the inspector you add more AudioSources, with an assigned AudioClip or not. Then in Awake() you can test for the clip name and get the AudioSource for each clip and then use that for playing sounds later.

 AudioSource oASThruster;
 AudioSource oASGeneral;
 
 private void Awake()
 {
     foreach (AudioSource source in GetComponents<AudioSource>())
     { //we have 2 audio sources
         if (source.clip!=null && source.clip.name.Equals("thruster"))
             oASThruster = source; //this is used only for thruster
         else
             oASGeneral = source; //this one is used for all different one shot sounds
     }
 }

Also a tip for stopping sounds... when you stop a sound abruptly it can introduce a clicking noise. Instead of stopping the sound, I set bThrusterFadeOut to true.

 bool bThrusterFadeOut = false;
 
 void Update()
 {
     //fade out some sounds over ~100ms, to avoid unwanted clicking noise
     if (bThrusterFadeOut)
         oASThruster.volume *= 0.8f;
 }
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 Casiell · May 19, 2021 at 08:33 PM

I had the same problem and I solved it by setting clip to null and calling Play. This will stop playing your previous clip and start playing null which is nothing. In my case it worked, because I changed the playing clip constantly anyway.

You may try just pausing the clip, Pause shouldn't affect OneShot clips. But this way the sound won't start from the beginning the next time you play it, so that may be a problem

You could also just have 2 audio sources. One for the sound playing when holding space and another that you use only for one shots. I think this is how I would've done it probably.

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

156 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

Related Questions

How to play sound after another with one button (and after full round continue from where the clip stopped)? 0 Answers

Problem with playing sound on Trigger-Enter 2 Answers

How to get decibel's value of game's sound? 0 Answers

Play audio at consistent volume 2 Answers

AudioSource won't play 2 Answers


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