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 /
avatar image
1
Question by CalledToGaming · Apr 19, 2010 at 10:21 PM · destroyhealth

How do I destroy an object when a certain number is reached? (Hit Points)

I'm trying to work on some basic AI and such. I just started learning it. Right now I'm trying to kill off an enemy (a box at this point) by reducing it's health to 0. When it reaches 0 or less, I want the object to be destroyed, but it's not working. Eventually I'll add things that modify the amount of dmg an enemy receives based off attack and such, but this little problem is holding up the whole operation.

Here's what I have JavaScript:

var Health = 100;

function update() { if (Health <=1) { Destroy (gameObject); }

}

function OnMouseUp() { Health -= 10; }

Unless I'm mistaken, the basic idea is there, but I'm missing something. The hit points drop correctly, but when it reaches 0 or less, the object still remains.

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

Answer by Eric5h5 · Apr 19, 2010 at 11:13 PM

The update function won't run automatically unless it's correctly named ("Update", not "update"). However, there's no need for that to be in Update in the first place, because Update runs every frame and you only need to check when the health actually changes. (Also, if you want it to be destroyed when the health is 0, it needs to be <= 0, not <= 1, which would destroy it if the health was 1.)

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 CalledToGaming · Apr 20, 2010 at 02:53 AM 0
Share

Thanks. Amazing what a typo can do with scripting...

avatar image Albert · Apr 20, 2010 at 07:12 AM 0
Share

@ Eric5h5, you said it doesn't have to be in Update? so is it better in Fixed Update? or where is the best to put that? :)

avatar image Eric5h5 · Apr 20, 2010 at 03:03 PM 0
Share

@Albert: FixedUpdate is only for physics. Just have a function that's called when the health is changed.

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

No one has followed this question yet.

Related Questions

Blocking particles then destroy then reduce health 0 Answers

Why does this do nothing (Health Script) 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone explain the destroy () command? 1 Answer

Destroy Scripting Help 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