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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by mattlewis83 · Jul 03, 2013 at 06:39 PM · destroyenemyhithits

How do I make it so my enemy is hit 8 times and is destroyed?

i know there are other posts about this... but i have scoured the internet and have found nothing that solves my problem. i have a bullet prefab that my character shoots with a altered script i used for picking up items and destroying them. basically when the bullet prefab hits the character he is destroyed after one hit. i need it so he can take multiple hits and then destroy. it seems simple enough...but i just cant get it. i have tried for a week now and my brain is melting. here is the script:

  #pragma strict
 
  private var score : int = 0;
     
  var counter = 0;
     
  var guiScore : GUIText;
     
  var enemygrunt : AudioClip;
     
  function Start () {
      guiScore.text = "KILLS: 0";
  }
 
  function OnCollisionEnter(col : Collision){
     
     if(col.collider.name == "ClodHopplerPrefab"){
     
         AudioSource.PlayClipAtPoint(enemygrunt, transform.position);
     
         counter +=1; 
     
     
         if (counter ==2);
     
     
         Destroy(gameObject); 
             
             
             
     
         score += 1;
     
         guiScore.text = "KILLS: " + score;
     
         print("collide with enemy"); 
     
     
     }
  }


any help would be great. i just cant seem to figure out what to do. i know this is a script for my "bullet" prefab...but do i need to add anything to my enemy to have it work? thanks again. -i'm dying over here.

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 HypoXic5665 · Jul 03, 2013 at 06:43 PM 0
Share

You might want to format your code using the 101010 button so that your script is readable for other users.

avatar image mattlewis83 · Jul 04, 2013 at 03:46 AM 0
Share

any ideas? it is 6 hours later and i still haven't been able to get this working.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Jul 03, 2013 at 06:45 PM

Have a script on the enemy with:

 //Health.js

 var health :int = 8;
 
 function Damage(damage:int){
     health -= damage;
     if(health <= 0)Destroy(gameObject);
 }

Then on the projectile or whatever hitting the enemy:

 //Bullet.js

 var damage:int = 1;
 
 function OnCollisionEnter(col:Collision){
    if(col.gameObject.tag == "Enemy"){
       var script = col.gameObject.GetComponent(Health);
       if(script != null)script.Damage(damage);
    }
 }

in C#

 public interface IDamageable{ void Damage(int damage); }
 public class HealthController:MonoBehaviour, IDamageable{
    int health = 8;
    public void Damage(int damage)
    {
        health -= damage;
        if(health <= 0) { Destroy(gameObject); }
    }
 }
 
 public class Projectile:MonoBehaviour{
     [SerializeField] private int damage = 1;
     void OnCollisionEnter(Collision col){
           if(col.gameObject.CompareTag("Enemy")){
                   IDamageable script = col.gameObject.GetComponent<IDamageable>();
                   if(script != null) { script.Damage(this.damage); }
           }
     }
 }


Comment
Add comment · Show 9 · 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 mattlewis83 · Jul 03, 2013 at 09:37 PM 0
Share

im getting an unknown identifier "health" hmm... i tried making health a variable but i get the same error.

avatar image mattlewis83 · Jul 03, 2013 at 09:48 PM 0
Share

i haven't tried it yet but will in a few $$anonymous$$utes. thank you so much for responding! i have been having so much trouble with this and making animated prefabs. i made a springboard that has a collision trigger on it that cues the animation for the top of the springboard and then a plane without the mesh renderer on to actually lift my character. i cant seem to be able to put the whole thing into a prefab. it doesn't animate anymore.i guess that will be a new topic.

avatar image mattlewis83 · Jul 03, 2013 at 09:49 PM 0
Share

Assets/bulletusewithenemy.js(5,48): BCE0005: $$anonymous$$ identifier: 'Health'. i got this error. i am new to java so be kind. is this because there is no variable "health"?

avatar image Lo0NuhtiK · Jul 04, 2013 at 03:51 AM 0
Share

Did you put the stuff @fafase posted in two different scripts, and name the health script "Health" ?

avatar image mattlewis83 · Jul 04, 2013 at 05:07 AM 0
Share

actually i didn't name it health. good call. i will try that. i am stupid. sorry. i really appreciate the help. i am trying very hard to make this game come together. thanks for all the help.

Show more comments

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

19 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 avatar image avatar image avatar image avatar image

Related Questions

Destroy and Spawn an Enemy 1 Answer

Melle system destroy game object not working despite no errors and declining health 1 Answer

Player not getting destroyed when touched by enemies 4 Answers

change script so enemy car gets destroyed when collided with player car 0 Answers

Enemy Health Bar 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