Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Sealmeister · Aug 11, 2015 at 10:59 AM · soundlooponce

Play sound once when variable is less than 0

So I have barricades that can be broken. When they are, I want them to play a sound ONCE. The problem is that the sounds is being looped instead of being played once. This is because the code states:

if (barricadeHealth <= 0){ playsound; }

How do I play the sound once every time a barricade goes below 0 hp?

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
0

Answer by NiloBR · Aug 11, 2015 at 11:28 AM

u can look at c# events.... you define a event like "onBarricadeDestroy", and subscribe to those events.... so you can execute some logic once they occur.... https://msdn.microsoft.com/pt-br/library/awbftdfh(v=vs.120).aspx

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 KdRWaylander · Aug 11, 2015 at 11:29 AM

Hi,

What you're looking for is the PlayOneShot() function :) Here you go: http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html

And make sure that there is no loop property or so checked in the inspector of your audio file !

Comment
Add comment · Show 3 · 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 Sealmeister · Aug 11, 2015 at 11:56 AM 0
Share

This is my code, the sounds gets looped because of update, but I need to have it in update to check hp. Sound is there aswell, therefore the loop. private SpriteRenderer sR; private HealthController hC; private AudioSource aS; private PolygonCollider2D polyC; private PlayerController playerC;

     //variables
     float maxHp;
     float timeToSound = 0f;
     float tempHp = 0f;
     bool playSound = false;
 
     // sound
     public AudioClip hammering;
     public AudioClip breaking;
 
     // Use this for initialization
     void Start () {
         aS = gameObject.GetComponent<AudioSource> ();
         polyC = gameObject.GetComponent<PolygonCollider2D> ();
 
         sR = gameObject.GetComponent<SpriteRenderer> ();
         hC = gameObject.GetComponent<HealthController> ();
 
         maxHp = gameObject.GetComponent<HealthController> ().currentHealth;
         tempHp = maxHp;
         //Debug.Log (maxHp * 0.7);
     
     }
     
     // Update is called once per frame
     void Update () {
     
         // When wood is repairing
         if (tempHp < hC.currentHealth && timeToSound <= 0) {
             aS.pitch = Random.Range(1f, 1.21f);
             aS.PlayOneShot(hammering);
             tempHp = hC.currentHealth;
             timeToSound = 4f;
         }
 
         if (timeToSound > 0) {
             timeToSound -= Time.deltaTime;
         }            
 
         if (hC.currentHealth <= 0) {
             if (aS.isPlaying == false){
                 //aS.clip = breaking;
                 aS.loop = false;
                 aS.PlayOneShot(breaking);
             }
             polyC.enabled = false;
 
         } else {
             polyC.enabled = true;
         }
     }
 
avatar image KdRWaylander · Aug 11, 2015 at 01:35 PM 0
Share

then add a boolean "isToBePlayed" and do something like this:

 bool isToBePlayed = false;
 
 /* ...
 Somewhere in your code your turn that boolean to true
 when you want to "allow" the sound to be played
 ... */
 
 void Update () {
    if (!aS.isPlaying && isToBePlayed){
       aS.PlayOneShot(breaking);
       isToBePlayed = false;
    }
 }
avatar image Sealmeister · Aug 11, 2015 at 01:37 PM 0
Share

Thank you! This worked :)

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

27 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

Related Questions

Collision with same gameobject only once 2 Answers

Audio issue - Trying Waitforseconds 0 Answers

Gunshot Sound Playing Too Many Times 1 Answer

Play sound ONCE! 3 Answers

trouble looping through voicelines using Coroutine 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