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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
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
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 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
0

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

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

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

weapon collision with characters 1 Answer

Multiple Cars not working 1 Answer

Character controller mess up damage 3 Answers

Add Health on Pickup to Decaying Health,Make a collision with an object add health 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