• 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 tonydemot · Sep 22, 2012 at 09:53 PM · instantiatedamagehealth

Enemy Health Damage

So i have a game where enemy's spawn and walk towards you and you have to shoot them, only problem is all enemy's in the scene have there health going back upto 100 when another enemy is instantiated the scripts look like t$$anonymous$$s.

 function Start () {
 CurrentHealth = FullHealth;
 }
 
 function Update () {
 Debug.Log(CurrentHealth);
 
 if(CurrentHealth < 0){
 CurrentHealth = 0;
 }
 
 if(CurrentHealth == 0){
 Destroy(gameObject);
 }
 
 }
  function DamageHealth(damage : int){
 CurrentHealth -= damage;
 }


and on the player i have t$$anonymous$$s to damage the zombie's

 var DamageScript: ZombieHealth = GameObject.FindWithTag("Zombie").GetComponent(ZombieHealth);

now i know what the problem is, its because all the zombies when instantiated have the same script and tag but my question is how do i solve t$$anonymous$$s? will i have to have different scripts for each zombie because im planning to instantiate loads at a time.

Comment
Add comment · Show 2
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 T27M · Sep 22, 2012 at 11:52 PM 0
Share
avatar image tonydemot · Sep 23, 2012 at 12:09 AM 0
Share

2 Replies

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

Answer by aldonaletto · Sep 23, 2012 at 12:27 AM

That's not the cause of your problem: I suspect you've declared CurrentHealth static, what makes it common to all zombies. If so, remove the static keyword - t$$anonymous$$s way there will be a local CurrentHealth for each zombie.
But the way you're applying damage is wrong too, as you've suspected. You must somehow get a reference to the zombie you've shot in order to apply damage to the right monster. If you're shooting with Raycast, get the reference from the RaycastHit structure, like t$$anonymous$$s:

   var $$anonymous$$t: RaycastHit;
   if (Physics.Raycast(..., $$anonymous$$t)){
     // somet$$anonymous$$ng was $$anonymous$$t - try to get the ZombieHealth script:
     var dmgScript: ZombieHealth = $$anonymous$$t.transform.GetComponent(ZombieHealth);
     // if the victim has such script, apply damage 10:
     if (dmgScript) dmgScript.DamageHealth(10);
   }

If you're $$anonymous$$tting the zombie with a projectile, get the reference in OnCollisionEnter (in the bullet script):

 function OnCollisionEnter(col: Collision){
   // try to get the ZombieHealth script:
   var dmgScript: ZombieHealth = col.transform.GetComponent(ZombieHealth);
   // if the victim has such script, apply damage 10:
   if (dmgScript) dmgScript.DamageHealth(10);
   Destroy(gameObject); // destroy the bullet
 }
 
   
 
Comment
Add comment · Show 4 · 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 tonydemot · Sep 24, 2012 at 09:03 PM 0
Share
avatar image CG-DJ · Jul 17, 2013 at 02:57 AM 0
Share
avatar image Casper_Stougaard · Aug 22, 2013 at 07:46 PM 0
Share
avatar image aldonaletto · Aug 23, 2013 at 02:36 AM 0
Share
avatar image
0

Answer by kookabara · Nov 13, 2013 at 10:59 AM

bullets need a multiple tagging from different element and different prefab effects from what i experience. i instantiate multiple items on their respective tags so i know what im $$anonymous$$tting and t$$anonymous$$s really prevent the triggering objects to activate at real time.

Comment
Add comment · 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

15 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

Related Questions

Damage not triggering in build 1 Answer

Prefab shooting damage/health? 1 Answer

Problem with health bar 1 Answer

Damage script is screwed up...? what to do? 1 Answer

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 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