Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
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 this.

 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 this 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 this? 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

How are you instantiating the zombies? When you deal damage are you just pressing a button that will deal damage to all of them?

avatar image tonydemot · Sep 23, 2012 at 12:09 AM 0
Share

no im shooting at them with a gun, i also am instantiating the zombies on a timer so every 5 seconds one spawns.

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 - this 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 this:

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

If you're hitting 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

ahh thank you this helped :D

avatar image CG-DJ · Jul 17, 2013 at 02:57 AM 0
Share

Ok, I'm the type of person that wants to know why everything works. This fixed my problem, but why doesn't it work the other way. There is a script there, so why doesn't it complete the function with out the extra if statement? Thanks for the help!

avatar image Casper_Stougaard · Aug 22, 2013 at 07:46 PM 0
Share

This was of great help for me too. Thank you very much :)

avatar image aldonaletto · Aug 23, 2013 at 02:36 AM 0
Share

@CG-DJ: the extra if checks whether the object hit has the script ZombieHealth, and if so calls its function DamageHealth. Without this if that line would be just dmgScript.DamageHealth(10);, but you would get Null Reference errors if the projectile hit a wall, the ground or any object that doesn't have the script DamageHealth.

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 hitting and this 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.

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 script is messed up, how to fix 1 Answer

How to create/fix fire damage script???? 1 Answer

Prefab shooting damage/health? 1 Answer

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 2 Answers

Damage not triggering in build 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