• 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
Question by Dhicci · Apr 21, 2016 at 05:17 PM · c#collisionvariablesdamagehealth

Take health from enemy

So...In my game, my fireBall particlesystem has to take as much health (stored in enemy) as there is damage (stored in fireBall) from enemy. The collision happens, but no health gets taken away.

 public class fireball : MonoBehaviour {
     public float speed=1f;
     public float damage = 25f;
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 transform.Translate(0,0,speed);

 }
 void OnParticleCollision(GameObject other)
 {
     if (other.transform.tag == "Enemy")
     {
         
         if (other.GetComponent<removeHealth>())
         {
             Debug.Log("collided");
             other.GetComponent<removeHealth>().health -= damage;
         }
     }
 }

 }

Enemy has this script.

 public class removeHealth : MonoBehaviour {
     public float health = 100f;
     void Start()
     {
        gameObject.tag = "Enemy";
     }
     void Update()
     { }
 
     public void RemoveHealth()
     {
         if (health < 1)
         {
             Debug.Log("YEY");
             Destroy(gameObject);
         }
     }
 }




Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by KidRigger · Apr 21, 2016 at 07:32 PM

The RemoveHealth method is not being called so call it from Update() of the enemy or call it from fireball. Also, I would rather make health private and reduce it through a public method.

Comment

People who like this

0 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 KidRigger · Apr 21, 2016 at 07:34 PM 0
Share

Although I'm making an assumption that object is losing health but not dying since you aren't monitoring health.

avatar image Dhicci · Apr 24, 2016 at 12:02 PM 0
Share

Could you explain this "monitoring health".

avatar image Dhicci · Apr 24, 2016 at 12:06 PM 0
Share

thx! I am really grateful. I moved RemoveHealth to Update and it worked. Really, Thank you!

avatar image

Answer by JackTheKreator · Apr 21, 2016 at 06:13 PM

If your enemy has multiple GameObjects as children, the ball may be colliding with one of the parts that doesnt contain the script. Try the GetComponentsInChildren function or the GetComponentsInParent method. I had the same problem in my game.

Comment

People who like this

0 Show 0 · 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

Answer by F-R-O-S-T-Y · Apr 24, 2016 at 02:41 PM

KidRIgger's answer works, however it is quite inefficient. You are running RemoveHealth every update, which is not necessary. You should run it only when health is deduced. Even your function name says "Remove Health" not "CheckHealth".

You should instead call the RemoveHealth() function only when collision was detected. And in RemoveHealth() function reduce health and then check if health is below 1. If it is, destroy the object.

So in fireball class:

 void OnParticleCollision(GameObject other)
  {
      if (other.transform.tag == "Enemy")
      {
          
          if (other.GetComponent<removeHealth>())
          {
              other.GetComponent<removeHealth>().RemoveHealth(damage);
          }
      }
  }

And in removeHealth class change the function RemoveHealth() to accept a float paramater "damage".

 public void RemoveHealth(float damage)
      {
          health -= damage;
          if (health <= 1)
          {
              Destroy(gameObject);
          }
      }

Also, it is quite common and a good habit to write class names in capital letters. So your class should be called RemoveHealth instead.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My player HP are get decreased from several GameObjects that has is Trigger on 0 Answers

My Photon health script doesnt work 2 Answers

ScriptableObject Referencing 1 Answer

Health Script/Targeting system c# 2 Answers

hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great. 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