Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by oskar-s · Feb 13 at 02:01 PM · componentinheritanceclassesarchitecturestructure

Composition: Multiple actions on death

Hello, Trying to understand Composition based design a little more.

I have a Health and an abstract Death Class. I inherite different child classes from the DeathClass like GameOverOnDeath, ExplodeOnDeath, DropItemOnDeath. Now i want to have an enemy to explode on death but to also drop some items on death. Whats the best approach top this?

(Other Question: Is there a good comprehensive guide to Composition based design WITH UNITY? Can't find any useful resources)

Health.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Death))]
 public class Health : MonoBehaviour
 {
     private Death death;
 
     [SerializeField] private int startingHealth;
 
     private int health;
 
     private void Awake()
     {
         death = GetComponent<Death>();
     }
 
     public void TakeDamage(int amount)
     {
         health -= amount;
         if (health <= 0f)
         {
             Die();
         }
     }
     public void Heal(int amount)
     {
         health += amount;
         if (health > startingHealth)
         {
             health = startingHealth;
         }
     }
     public void Die()
     {
         death.OnDeath();
     }
 }


Death.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public abstract class Death : MonoBehaviour
 {
     public abstract void OnDeath();
 }

And as example here is one of my inherited death child classes "GameOverOnDeath"

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameOverOnDeath : Death
 {
     public override void OnDeath()
     {
         LevelManager.Instance.GameOver();
     }
 
 }
 
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
0
Best Answer

Answer by Captain_Pineapple · Feb 13 at 03:06 PM

In general i'd say your approach is already good.

i'd do it a bit different as the Death class does not really contain enough to legitimize its own Monobehaviour.

So basically I'd give all your current functionality to the Health class and then you extend your class by an OnDeathCallback like this:

 public System.Action OnUnitDiedCallback;

To this you can subscribe from other classes:

 GetComponent<Health>().OnUnitDiedCallback += someFunctionToTrigger;

So for example if we assume you have some GameManager that detects the game end you could have:

  playerCharacter.GetComponent<Health>().OnUnitDied += gameEnd;

and so on. The actual call to this is done like this:

 public void Die()
 {
      OnUnitDiedCallback?.Invoke();
 }

In case you don't know the ? operator: that checks the OnUnidDiedCallback if it is null or not.


Hope this helps. Let me know if something was unclear.

Comment
Add comment · Show 2 · 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 oskar-s · Feb 13 at 03:56 PM 0
Share

Okay, thank you. That sounds like a good approach. But with this i am not sure how to handle the other cases. So if a unit does different thing on death ( drops an item, explodes and plays a sound) how to achieve that? Have an item drop manager for the item drop maybe? But how to play sounds and effects in a composition based system? Should the "Health.cs" have a Slot for a deathSound, a deathFX, a damageSound and a damageFX? Then it would check if those are not empty and play them? Or make another component "FXPlayer" and ''AudioPlayer" and check if those have a filled deathFx and deathSound slot?

BTW: Didn't know the ? Operator but now i know. Thank you!

avatar image Captain_Pineapple oskar-s · Feb 13 at 05:16 PM 0
Share

You can subscribe as many functions to OnUnitDiedCallback as you like.

     OnUnitDiedCallback += centralSoundManager.PlayDeathSound;
     OnUnitDiedCallback += dropManager.DropItem;
     //and so on...

To have a centralized drop function would require however that you know where to drop something so your OnUnitDiedCallback Action might need a parameter so your Action might have to look like something like this:

    public System.Action<Health> OnUnitDiedCallback;

So your call in your health script is then:

 public void Die()
  {
       OnUnitDiedCallback?.Invoke(this);
  }

Like that you can then on the other side access the transforms position from the given health script.

Regarding the sounds i personally would give that to my unit behaviour script. So when you have your unitbehaviour that would have some things assigned like what sound to play when it dies. So in your Awake you can just the same subscribe to the ondeath callback of the Health script.

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

136 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

Related Questions

Game architecture best practices 3 Answers

How unity works under the hood? 0 Answers

Ability System with Modular Targeting: having trouble finding a good way to map between input components and effect components 0 Answers

Class inheriting Runtime Classes 1 Answer

Inheritance and Component Types in C# 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