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 /
  • Help Room /
avatar image
0
Question by Ego65 · Apr 21, 2016 at 07:58 AM · audiosoundaudiosourceaudioclipplay

Need help: new sound isnt played correctly anymore

hey at all, i'm in trouble. i've added a sound to my ship energy shield. it should be played when the shield energy is less than 50%. i've done it the same way as i did with other game sounds and never had this prob before. all other sounds are working as expected. not sure but i think this is the first sound effect i want to add since i've upgraded to 5.3.2. no matter which import settings i use the best is just a very short scratch when the sound should be played. seems its being cut as soon as it starts to play.

here's the script: ( i think its ok )

 using UnityEngine;
 //using System.Collections;
 
 public class SchutzSchild_health : MonoBehaviour 
 {        
         public float Schutzschild_hitPoints = 1000f;
         public float Schutzschild_currentHitPoints;
         public float Schutzschild_curHealth = 100f;
         public float Schutzschild_maxHealth = 100f;
         public float Schutzschild_my_value ;
         public float repairvalue = 10.0f;
         public bool damaged = false;
         public GameObject destroyFX;
         public AudioSource WarningSound;
         // Use this for initialization
         void Start () 
         {
                 Schutzschild_currentHitPoints =Schutzschild_hitPoints;
                 WarningSound = GetComponent<AudioSource> ();
         }
         public void PlayerDamage(float amt)
         {
                 Schutzschild_currentHitPoints -= amt;
                 //Debug.Log (amt);
                 if(Schutzschild_currentHitPoints <= 0)
 
                 {
                         Schutzschild_currentHitPoints = 0;
                         gameObject.SetActive(false);
                         //Die();
                 }
         }
         void Die()
         {
                 if(gameObject.tag == "Player")
 
                 {
                         Instantiate (destroyFX, this.transform.position, this.transform.rotation);
                         Destroy(gameObject);            
                 }    
         }
 
         // Update is called once per frame
         void Update () 
         {
                 WarningSound = GetComponent<AudioSource> ();
                 //WarningSound.Stop();
                 AddjustCurrentPlayer_Health (0);
                 //Debug.Log(Schutzschild_curHealth + " %");
 
                 if(Schutzschild_curHealth <= 50.0f)
                 {
 
                         WarningSound.Play();
                 }
 
         }
         public void FixedUpdate()
         {
 
                 if(Schutzschild_curHealth < 100.0f)
                 {
                         damaged = true;
                 }
                 else
                 {
                         damaged = false;
                 }
                 if(damaged == true && Schutzschild_currentHitPoints > 0)
                 {
                         Schutzschild_currentHitPoints += repairvalue * Time.deltaTime;
                 }
                 else
                 {
                         return;
                 }
         }
         public void AddjustCurrentPlayer_Health (float adj)
         {
                 Schutzschild_maxHealth = Schutzschild_hitPoints / 10;
                 Schutzschild_curHealth = Schutzschild_currentHitPoints / 10;
 
                 Schutzschild_curHealth += adj;
                 if (Schutzschild_curHealth < 0)
                         Schutzschild_curHealth = 0;
 
                 if(Schutzschild_curHealth >  Schutzschild_maxHealth)
                         Schutzschild_curHealth =  Schutzschild_maxHealth;
 
                 if(Schutzschild_maxHealth < 1 )
                         Schutzschild_maxHealth = 1;
 
                 Schutzschild_my_value = Schutzschild_curHealth / (float)Schutzschild_maxHealth;
             
         }
 
         public void Repairshield()
         {
 
         
                         
                 
         }
 }  

i'm out of any idea how to solve it now so i would be thankfully for some help.

yours

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
1
Best Answer

Answer by Soraphis · Apr 21, 2016 at 10:51 AM

check your update method: you start playing the sound every frame, when your health is <50%

 if(Schutzschild_curHealth <= 50.0f){
     if(! WarningSound.isPlaying)
         WarningSound.Play();
 }


edit: not related to your question but:

Fixed Update()

why do you use fixed update? http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html http://forum.unity3d.com/threads/the-truth-about-fixedupdate.231637/

  • dont compare a boolean to "true". "b == true" does not make sense.

  • return as last statement is useless.

you could shrink the code to:

 damaged = Schutzschild_curHealth < 100.0f;
 if (damaged && Schutzschild_currentHitPoints > 0) {
     Schutzschild_currentHitPoints += repairvalue*Time.deltaTime;
 }

you can move all those line into "PlayerDamage", if this is the only point where the health get reduced, or move it into update.

AddjustCurrentPlayer_Health(float adj)

can be shrinked to:

     Schutzschild_maxHealth = Schutzschild_hitPoints/10;
     Schutzschild_curHealth = Schutzschild_currentHitPoints/10; // are you sure about this?

     Schutzschild_maxHealth = Mathf.Min(Schutzschild_maxHealth, 1);
     Schutzschild_curHealth = Mathf.Clamp(Schutzschild_curHealth + adj, 0, Schutzschild_maxHealth);

     Schutzschild_my_value = Schutzschild_curHealth/(float) Schutzschild_maxHealth;

if you dont know Mathf.Clamp check it out here: http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

Update() dont do this WarningSound = GetComponent<AudioSource>(); every frame, you already did it in start, no need to do it here.


i would have written it more like this: http://pastebin.com/KAwB9t4x btw: i think you've got many variable with no purpose

Comment
Add comment · Show 1 · 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 Ego65 · Apr 23, 2016 at 07:57 AM 0
Share

ABSOLUTELY STRANGE !
just one day later i set the code back to your suggestions ( like i did before ) and now its working like it should.
please dont ask why - i dont know it :)

finally once more many thx for your effords
yours

avatar image
0

Answer by Ego65 · Apr 22, 2016 at 09:36 AM

many thx Soraphis !
i've tried all your suggestions but nothing really helped. also your script you've kindly posted at pastebin.com but there i've got several errors and after i tried to fixed them the result was the same. the only way to get the sound to play and not only a short "scratch" seems to be :
if(Schutzschild_curHealth <= 50.0f)
`{ WarningSound.PlayOneShot(WarningSound.clip); }
but that's terrible and sure it doesn't stop.
so neither WarningSound.Play(); doesn't work, nor WarningSound.PlayDelayed(1.0f);
seems its a bug and i'm near to give up if there isnt a clear way to fix that prob. hard enough for beginner to learn programming but if you will have to study sound engenering as well thats to much for me :(

edited: i even attached the soundclip to another gameobject and this part of script and the result also the same

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

60 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

Related Questions

Second AudioClip won't play 0 Answers

Some sounds won't play 0 Answers

Audio is way too soft on mobile but alright in Unity Editor! 0 Answers

Audio Cutting Out Unexplainably 1 Answer

Audio is cut in script 0 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