• 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 Pong · May 09, 2011 at 06:00 PM · enemysounddead

enemy die sound problem

I have put a sound on the enemy health script and I write about if the health = 0, then play the sound. but the problem is it generate more than one sound when it's health is 0. Below is the script i write. How can I fix it?

var Health : int = 20;

var deadsound : AudioClip; var Playaudio : int = 2; var Recount : float; var effectsound : Transform;

function Start (){ animation["hit"].wrapMode = WrapMode.Once; animation["hit"].layer = 2; animation["die"].wrapMode = WrapMode.Once; animation["die"].layer = 2; Recount = 0; }

function Update () { if (Health == 0){ animation.Play("die"); Recount = 1; audio.PlayOneShot(deadsound); Destroy(gameObject, 3); } }

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

1 Reply

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

Answer by almo · May 09, 2011 at 07:42 PM

Maybe add

var PlayedDeathSound : bool;

function Start() { PlayedDeathSound = false; ... (the rest of your Start here) }

function Update() { if(Health == 0) { ... (rest of your death code here) if(!PlayedDeathSound) { PlayedDeathSound = true; audio.PlayOneShot(deadsound); } } }

But if you destroy the object when you play the sound, it means there's probably another issue with your code, and my solution will just patch the symptom without fixing the issue.

Comment
Add comment · Show 2 · 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 Pong · May 10, 2011 at 03:37 AM 0
Share

wow!!!$$anonymous$$any thanks for your help The problem have been solved.

avatar image almo · May 10, 2011 at 12:34 PM 0
Share

Sure, glad I could help. :)

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

No one has followed this question yet.

Related Questions

Play sound only once. 1 Answer

How do I get my enemy to scream when he is close to "Player" 1 Answer

Preserve data within a scene 1 Answer

Play sound when looking at enemy? 1 Answer

Enemy sound detection 5 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