• 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
Question by Fusionforce · Jun 29, 2016 at 10:12 PM · c#2d-platformersoundsdie

player death sound not playing

i got my hurt sound to work when ever i come into contact with a spike but am not really sure why my death sound is not working

 public class playerHealth : MonoBehaviour {
     // declareing health variables
     public float maxHealth;
     private float currentHealth;
 
     // declareing audio variables
     public AudioClip playerDieing;
     public AudioClip[] hurt;
     public GameObject deathFX;
 
     // delcareing component variables
     private playerController controllerMovement;
     public  AudioSource playerAS;
 
     // declareing hud variables
     public Image healthSlider;
     public Image damageScreen;
 
     private Color damagedColor = new Color(255f, 255f, 255f, 0.5f);
 
     private bool damaged = false;
     private float smoothColor = 5f;
 
     void Start () {
         // health instalization
         currentHealth = maxHealth;
 
         // component instalization
         controllerMovement = GetComponent<playerController>();
         playerAS = GetComponent<AudioSource>();
 
         // hud instalization
         healthSlider.fillAmount = maxHealth;
         damaged = false;
     }
     
     // Update is called once per frame
     void Update () {
         // if player is damaged then show the damage screen else make the damage screen disapper adjusting the alpha using lerp and deltatime
         if(damaged) {
             damageScreen.color = damagedColor;
         } else {
             damageScreen.color = Color.Lerp(damageScreen.color, Color.clear, smoothColor * Time.deltaTime);
         }
         // setting damaged equal to false
         damaged = false;
     }
 
     // controls if the player is being damaged if so then how much damage and what happens when the player takes damage
     public void addDamge(float damage) {
         if(damage <= 0) {
             return;
         }
         currentHealth -= damage;
         playerAS.PlayOneShot(hurt[Random.Range(0,hurt.Length)]);
         healthSlider.fillAmount = currentHealth / 100;
         damaged = true;
         if(currentHealth <= 0) {
             playerAS.PlayOneShot(playerDieing);
             makeDead();
         }
     }
 
     public void makeDead() {
         Instantiate(deathFX, controllerMovement.transform.position, transform.rotation);
         Destroy(gameObject);
     }
 }
 
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by jgodfrey · Jun 29, 2016 at 11:30 PM

I'd guess your game object is being destroyed before the sound finishes playing. So, you either need to 1) play the sound from some other class (like an AudioManager or similar), or 2) delay the destruction of your game object until after the sound has completed.

Option #1 is probably a better design, but also likely requires more radical changes to your current design.

For option #2, you might be able to get away with immediately destroying the components you don't need anymore (for instance, the renderer to "hide" the object). But, delay the destruction of the main object until the clip completes. You can pass in an optional time value to Destroy to delay the destruction for a predetermined amount of time. In your case, you could pass in the length of your audioClip (accessed via the "length" property of the audioClip) as the delay. Or, just pass in a static value that's "long enough" if you want...

Comment
Fusionforce
KaffeSumpen

People who like this

2 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

174 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 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

Right guys struggling here! new to code... My first object (power up) is being destroyed but no power function is being generated, second object and there after works perfectly. PLEASE HELP 0 Answers

How to easily move character specific amount up and down 0 Answers

One 2D Raycast Not Stopping Even Though Others Do? 0 Answers

Enemy Spawning Help (2D Platformer, C#) 2 Answers

how create EndLess game Like zombie Tsunami 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