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 /
  • Help Room /
avatar image
0
Question by Raresh · May 27, 2016 at 01:12 PM · c#

Managing taking damage.

Supposing we have different objects in our game, ships, bases, turrets, cargo pods etc. and projectiles, when a projectile hits something, how will it know what function to call in order for that object to take damage? Knowing that each object has a special manager, that implements an interface IDamageable let's say, and that it's always at the root of the object (but turrets will be children for example) and the collider isn't always in the same object as the manager, how can I handle the damage that the projectile does (along with other special attacks etc)? Also, the managers of each object would inherit from a base manager(if that helps at all)...

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

1 Reply

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

Answer by YoungDeveloper · May 27, 2016 at 01:42 PM

Why not implement from Monobehaviour base? On hit try to get the component, if it's not null then remove health. Here's and example.

 public class KillableBase: MonoBehaviour {
 
     [SerializeField]private int _health = 100;
 
     public void RemoveHealth(int amount){
         health -= amount;
     }
 
 }

 public class Ship: KillableBase{
     //members and methods for ship only
 }

  1. Now our ship is somewhere in the game

  2. It gets hit.

         int damage = 57;
         GameObject hitObject; //get our hit from raycast
     
         KillableBase killable = hitObject.getComponent<KillableBase>();
         
         if(killable  != null){
             killable.RemoveHealth(damage );
         }
    
    
    
    
    
    
    
    
    
    
    
    
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 Raresh · May 27, 2016 at 01:48 PM 0
Share

This part i kind of got working, the tricky part is finding the correct $$anonymous$$illableBase that should take damage. As i said in the question, it is not always in the same place as the collider, and there are multiple colliders and $$anonymous$$illableBase derived classes scattered all over an object.

avatar image YoungDeveloper Raresh · May 27, 2016 at 01:50 PM 0
Share

You have to form your own hierarchy logic and stick to it. I usually stick to adding such component to main parent gameobject only. So after collider hit i get it's root by transform.root and process that. Other, but awful solution would be looping all transforms of that trasform and checking all of them.

avatar image Pharaoh_ Raresh · May 27, 2016 at 02:38 PM 1
Share

I understand your concern. I always think the same way (what if...), even if I apply the same logic to every gameobject in the scene. The only way I have come about to appreciate and implement is GetComponentInChildren(true); if you have more than one, GetComponent*s*InChildren. If you are still unsure which gameobject would the message emitter be, a parent or a child, you can access the root transform and apply the aforementioned method. If you know it is a child, you can use GetComponentInParent.

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

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

unable to use dontdestroy on load 1 Answer

Trying to get audiosource to play once 1 Answer

MobController 1 Answer

C# enable script component 2 Answers

Dynamic Placement of Object Instantiate using RaycastHit 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