• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Can someone explain the destroy () command? 1 Answer

Why does this do nothing (Health Script) 2 Answers

Blocking particles then destroy then reduce health 0 Answers

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges