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
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
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
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
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                