• 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 Jamie 5 · Apr 04, 2011 at 08:14 AM · destroyvariableenemyhealth

Enemy Health Problems

Ok, I am creating a 3d shooter game and I have a player that can shoot 'bullets' at the enemies. I can simply use OnTriggerEnter on the bullet to detect whether it collided with an enemy and then destroy the enemy. But what I would like to do is set up the enemies to withstand 3 hits or something and then destroy the enemy.

I have tried adding a health variable and every collision subtract 1 from the variable until it reaches zero, but when I play the game the lives don't subtract and the enemy is never destroyed. So I set the Health variable to a static variable but then if I have 2 enemy prefabs on the scene it takes 3 hits between the 2 enemies to destroy them. Is there any way to limit the health to the enemy that was hit? below is my script.

//Define Variables var explosion : GameObject; var scoreadded : boolean = false; var health = 3; var obj : GameObject;

function OnTriggerEnter (theObject : Collider) { if(theObject.gameObject.tag=="enemy") { //Instantiate(explosion, theObject.gameObject.transform.position, theObject.gameObject.transform.rotation); //Destroy(theObject.gameObject); health -= 1; obj = theObject.gameObject; } }

function Update() { if (health <= 0) { Instantiate(explosion, obj.transform.position, obj.transform.rotation); Destroy(obj); } }

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

Answer by KeithK · Apr 04, 2011 at 08:48 AM

What you could do is call a function on your Enemy object rather, and have it track it's own health and when it should die, etc. You can also that way avoid checking every update if it should be killed and just do the check in the Damage function.

So in your Enemy object script. Not your bullet. I am just assuming the scrip name is "EnemyBehaviour", you can replace all occurences of "EnemyBehaviour" with whatever name your Enemy script actually has.

var health : int = 3;

function TakeDamage(Damage : int) { health -= Damage;

if (health <= 0) { Destroy(gameObject); } }

Then in your Bullet object script.

var damage : int = 1;

function OnTriggerEnter (theObject : Collider) { if(theObject.gameObject.tag=="enemy") { theObject.gameObject.GetComponent(EnemyBehaviour).TakeDamage(damage); } }

Give that a go and see how it works. I'm at work at the moment, so haven't tested it.

Also, let me just explain the result you were seeing with making the "health" variable static in your explanation.

Making a variable static, means that all instances of that object that are created share that ONE variable, the variable is only created once, when the first instance of the object is created, after that any other object instances created just reference the already created variable.

That is why when you minus one in one object, and minus one in another, those objects see their health as having gone down by 2, and not 1 as you expect.

I hope that makes some sort of sense.

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 Jamie 5 · Apr 04, 2011 at 09:50 AM 0
Share

thanks, it works perfectly. Your explanation made a lot of sense, thanks again!

avatar image KeithK · Apr 04, 2011 at 09:52 AM 0
Share

No problem :) Glad I could help.

avatar image branek42 · Oct 27, 2013 at 07:53 PM 0
Share

help me pls my Bullet not kill enemy :(

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

1 Person is following this question.

avatar image

Related Questions

Need help with AI script 1 Answer

Destroying Enemy Help 5 Answers

How can I lose health when my enemy collides with the player? 1 Answer

health code help 1 Answer

How can I randomize the speed of enemy movement? 2 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