Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bryangs · May 28, 2017 at 11:20 PM · audiosourcestopmakedont

Audio Source question

Oh well, my bad if this question was asked before. Actually also my bad for not knowing what to type as the title, but i have a noob question xD

I am working on a simple project to sharpen my skills with mobile, and i have a part on my project that requires the player to press the same button a lot of times continuously. Everytime you press the button, a sound is played. Naturaly, if i press the button again while the sound is still playing, it will stop and restart. But how would i cancel this effect? I wanted the sound to not stop when the button is touched, so if the audio has 5 seconds and you touched the button 10 times in 3 seconds, it would still play the audio 10 times. How could i do this? Can someone explain? Many thanks ^-^

Comment
Add comment · Show 5
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 arielsan · May 28, 2017 at 11:44 PM 1
Share

So you want the sound to enqueue multiple times or to play concurrently? For the second you need multiple audio sources, one for each you want to play concurrently.

avatar image Bryangs arielsan · May 28, 2017 at 11:49 PM 0
Share

Thanks, mate! Coding with two audio sources worked as expected =)

avatar image SohailBukhari Bryangs · May 29, 2017 at 06:41 AM 0
Share

Using 2 audio source is not the solution. Note time when you press button and after spending seconds(your desired seconds) again play sound if pressing button. You can use PlayOneShot if already sound playing.

avatar image Bryangs · May 29, 2017 at 06:28 PM -1
Share

Not really, i simply used:

 if (playedOne == false)
 {
      sources[0].Play();
      playedOne = true;
 }
 
 else
 
 if (playedOne == true)
 {
      sources[1].Play();
      playedOne = false;
 }
avatar image ishdagz · May 29, 2017 at 08:15 PM 0
Share

ins$$anonymous$$d of: Audiosource.Play()

use: Audiosource.PlayOneShot(yourSound). It will do what it seems you are trying to do. Caveat: you will have no control over the sound once it is played (like if you wanted to change pitch/volume via script), but it doesn't seem like that is something you need.

Check the docs for how to use volume parameter

https://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by · May 29, 2017 at 08:01 PM

You could Instantiate a prefab that contains the audio that you want to play and make it destroy it self when the playback is finished.

This is something I wrote sometime ago. I know that there are better ways of doing this. This script will destroy destroy the gameobject when the sound ends, and it accounts for the pitch value.

 public bool autoDestroy;
 
     AudioSource audioSrc;
     float startTime;
     // Use this for initialization
     void Start () 
     {
         audioSrc = GetComponent<AudioSource> ();
         if (autoDestroy) {
             startTime = Time.time;
         }
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(autoDestroy) {
             if(((Time.time * Mathf.Abs(audioSrc.pitch)) - (startTime * Mathf.Abs(audioSrc.pitch))) >= audioSrc.clip.length){
                 Destroy (gameObject);
             }
         }    
     }
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

71 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

Related Questions

Make ALL animations stop! 1 Answer

How can i make my 2D character not toppling over 0 Answers

Stopping AudioClip/AudioSource C# 0 Answers

Audio: Does Stop() affect PlayOneShot()? 2 Answers

Problem with many audio sources playing at the same time 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