Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
3
Question by superventure · Mar 01, 2011 at 10:28 PM · updatesoundplayonce

Play sound ONCE!

I need to trigger sound at the beginning of an action, and for it to play ONCE, and one time only. The thing is, I have all of my actions in an Udate function! I can't seem to force a sound clip to play through only one time- update keeps calling every frame. I cant link it to a coroutine either, because my intial commands are in Udate. I saw a post explaining something along the lines of using

animation["action"].time 

but I can't seem to get it going. I've tried

}if (animation["attack"].time ==.1){
    audio.PlayOneShot(blast);

but that doesn't trigger it all.

I just need sound to play once on an action! Does any know of a work around? My models are from 3ds max, and and animation events seem over the top for something so simple. Thanks in advance!

Comment
Add comment · Show 4
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 Bunny83 · Mar 01, 2011 at 11:30 PM 0
Share

Just a little hint: something like if (animation["attack"].time ==.1) is like playing lotto. A float can have very small variances. 0.1 is not the same as 0.10001 or 0.099999 . $$anonymous$$aybe use an AnimationEvent ? http://unity3d.com/support/documentation/Components/animeditor-AnimationEvents.html

avatar image Bunny83 · Mar 01, 2011 at 11:35 PM 0
Share

Ohh, just read you don't want to use AnimationEvents, well if your condition is an exact animation time i strongly recommend them. You can of course check if the time is greater than .1 and in addition do the check that efge mentioned.

avatar image superventure · Mar 02, 2011 at 02:41 AM 0
Share

It does make since to use an exact animation time, but (animation["attack"].time ==.1) doesn't trigger the sound. If I say to use any time greater than 1, then sound will play every second still. Any Ideas?

avatar image superventure · May 20, 2011 at 11:00 PM 0
Share

Thanks, but we are overlooking it... It really needs '0' for what to check for with time! See me answer, and thank you for helping.

3 Replies

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

Answer by superventure · May 20, 2011 at 10:59 PM

Found a solution. I was close in my original attempt:

 if(animation["x"].enabled && animation["x"].time == 0)
 audio.clip = xsound;  
 audio.Play(); 

When you think about it, all sound clips are equal to zero at one point, so this will be a sure thing to check ONCE and one time during the life of the animation.

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
10

Answer by efge · Mar 01, 2011 at 11:04 PM

To play a sound once at the beginning you could play it in the function Start() or set your AudioSource in the inspector to PlayOnAwake true, Loop false.

To play a sound once during Update() use a boolean variable or instantiate a GameObject with an AudioSource with the same settings.

function Start() { soundplayed = false; }

function Update () { if (!soundplayed) { audio.PlayOneShot(blast); soundplayed = true; } ... }

Comment
Add comment · Show 5 · 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 Chris 33 · Apr 26, 2011 at 05:07 AM 0
Share

this is great! would not innovate this way on my own.

avatar image Fox-Handler · Jul 01, 2014 at 10:15 AM 0
Share

Thanks this helped me out

avatar image jeremyn · Jul 02, 2015 at 12:08 PM 0
Share

This solved my problem too!

avatar image Alpagu · Nov 02, 2015 at 12:39 PM 0
Share

Thank you so much, great solution.

avatar image UnityFan18 · May 02, 2017 at 12:22 AM 0
Share

You are super awesome! This code helped me to figure out how to play a jump landing sound once the player presses the jump button. Thank you so much!!!!!!

avatar image
0

Answer by cody01100011 · Jun 30, 2021 at 10:13 PM

sourse code ( https://docs.unity3d.com/ScriptReference/AudioSource.Play.html )

  public  AudioSource My_audio;
 
 void Start(){
 My_audio.Play();
 }
 void Update{
 if (distanse > 10) { My_audio.Pause (); }
 if (distanse < 10) { My_audio.UnPause (); }
 }
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

Audio issue - Trying Waitforseconds 0 Answers

Open Door Once But Sound Plays again when i trigger at the door 3 Answers

Play audio.PlayOneShot() once in an Update() function 2 Answers

make sound work 0 Answers

Not playing entire animation when it is called. 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