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 Corentin_Fousset-Martial · Sep 13, 2018 at 04:34 PM · arrayaudiosoundaudiosourceaudioclip

Play sound from an array, not random, and only once

Hi, I'm having trouble to make something "simple" work properly. I've created a small script to create an array of sound (only 2 sounds for now but may grow further) and play one of the sound accordingly to the value of a variable named "combo". For exemple, if combo is equal to 1.4f, it plays the sound n°1 once; if combo => 1.7f it plays sound n°2 only once. But it's actually not working and plays both sounds over and over when combo is => 1.7f. I could'n find a proper solution for now.

here's my script:

 using System.Collections;
 using UnityEngine;
 
 public class ComboSounds : MonoBehaviour {
     
     public AudioClip[] audioClipArray;
     private AudioSource source;
 
     void Start () {
         source = gameObject.GetComponent<AudioSource> ();
     }
     
     void Update () {
         if (ComboController.combo == 1.4f) 
         {
             source.clip = audioClipArray[0];
             source.PlayOneShot (source.clip);
             source.Play();
         }
 
         if (ComboController.combo >= 1.7f) 
         {
             source.clip = audioClipArray[1];
             source.PlayOneShot (source.clip);
             source.Play();
         }
     }
 }
 
 

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 TreyH · Sep 13, 2018 at 07:49 PM 1
Share

Floating point equality comparison isn't guaranteed. I think you mean to do something like this:

 float combo = ComboController.combo;
 
 if (combo >= 1.7f) 
 {
     source.clip = audioClipArray[1];
 }
 else if (combo >= 1.4f ) 
 {
     source.clip = audioClipArray[0];
 }
 else 
 {
     return;
 }
 
 source.PlayOneShot (source.clip);


Although, I think you'd also need something there to keep track of which "tier" combo you're working on next, to keep the sounds unique to those new player situations and not constantly playing throughout their experience.

3 Replies

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

Answer by iHaveReturnd · Sep 13, 2018 at 07:43 PM

You're running this in the update, so it makes sense that you're getting the looping audio when the value is 1.7f. Every frame that it's 1.7f its going to call your source.Play() and start the audio again You don't want both of those there either, you likely only one source.PlayOneShot with how you've described it, so remove source.Play() as dan_wipf suggested. dan_wipf's comments are still important changes to make, even if its still not working.

If you only want them to play once ever, you could use a flag type system. public class ComboSounds : MonoBehaviour {

      public AudioClip[] audioClipArray;
      private AudioSource source;
 
      private bool played14 = false;
      private bool played17 = false;
  
      void Start () {
          source = gameObject.GetComponent<AudioSource> ();
      }
      
      void Update () {
          if (ComboController.combo == 1.4f && played14 == false) 
          {
               played14 = true;             
               source.clip = audioClipArray[0];
              source.PlayOneShot (source.clip);
              source.Play();
          }
  
          if (ComboController.combo >= 1.7f && played17 == false) 
          {
              played17 = true;
              source.clip = audioClipArray[1];
              source.PlayOneShot (source.clip);
              source.Play();
          }
      }
  }

You could also reset those variables to allow them to play again later if you wanted. Realistically if you're only wanting this event to happen once, you shouldn't check for it in the update. Some other event should be calling an audio play function when that specific thing happens.

There's a few other ways you could go about it depending on what your end goals are but hopefully that helps some.

If you're still having it play when it shouldn't it'd be helpful to see your inspector view of it, and the settings of the audio file in the inspector too.

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
2

Answer by dan_wipf · Sep 13, 2018 at 04:44 PM

delete the phrase source.Play(); and in inspector check that playonawake and loop in your audiosource is false(unchecked)..

Comment
Add comment · Show 2 · 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 Corentin_Fousset-Martial · Sep 13, 2018 at 05:13 PM 0
Share

I tried but I have still the same problem unfortunately, the first sound doesn't even play when "combo" == 1.4

avatar image dan_wipf · Sep 13, 2018 at 07:10 PM 0
Share

my bad, i think you’re calling the function playoneshot multiplebtimes. because the bool stays true until the value changed.. you have to make a workaround, so it’s going to be a one time event!.. hope you get an idea

avatar image
1

Answer by Corentin_Fousset-Martial · Sep 13, 2018 at 09:31 PM

Thanks dan_wipf and thanks iHaveReturnd ! Both advises helped me to get rid of the problem ! I've learned another thing today !

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

133 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

Related Questions

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

AudioSource won't play 2 Answers

Audio: -3db automatic attenuation on any audio playing? 0 Answers

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

Make the audio manager select from an array of sounds 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