• 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 ravengamed · Oct 26, 2022 at 05:35 PM · delegatefunction calldelegatesget component

Is their any way to Invoke a specific delegate

Let's say i have a Player script attach to player and then an Enemy script w$$anonymous$$ch is attach to all enemies in the scene like 20 enemies. If player killed one enemy then i have to call the EnemyDied Function. If i make a delegate EnemyKillDelegate w$$anonymous$$ch will invoke when player killed any enemy then it will call the registered function EnemyDied, but in t$$anonymous$$s case EnemyDied of every enemy will call w$$anonymous$$ch is definitely not what i want. Is there a way to specifically call the EnemyDied function of the enemy script w$$anonymous$$ch is died. I know i can GetComponent of Enemy w$$anonymous$$ch is died then call the function but is there any different way to do t$$anonymous$$s

 public class Player : MonoBehaviour
 {
     //T$$anonymous$$s Scrip is attach to only Player
     public static event Action EnemyKillDelegate;
 
     void EnemyDied()
     {
         EnemyKillDelegate?.Invoke();
     }
 }
 
 
 public class Enemy : MonoBehaviour
 {
     //T$$anonymous$$s Scrip is attach to every enemy in the scene
     private void OnEnable()
     {
         Player.EnemyKillDelegate += EnemyDied;
     }
 
     private void OnDisable()
     {
         Player.EnemyKillDelegate -= EnemyDied;
     }
 
     void EnemyDied()
     {
         Debug.Log("I am Dead");
     }
 }

Comment
maateen

People who like this

1 Show 3
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 Llama_w_2Ls · Oct 26, 2022 at 08:58 PM 0
Share

Why are you using a delegate in the first place? You could simply get the enemy script of the enemy you kill, and call the public method EnemyDied(). @ravengamed

avatar image ravengamed Llama_w_2Ls · Oct 28, 2022 at 01:23 PM 0
Share

Yes, I can do that and currently I am doing the exact same thing which you said in my game but I am thinking if i can do this in delegate or any other better approach if any you can suggest? because if i have to perform any thing like health reduce, health increase etc. on a specific enemy i have to get enemy script and then call that public function

avatar image Llama_w_2Ls ravengamed · Oct 28, 2022 at 02:19 PM 0
Share

That's the best approach. If you want to reduce health, you'll have to create another event, same as creating another method and just calling it. It's unnecessary.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Oct 29, 2022 at 01:50 AM

What @Llama_w_2Ls said in the comments above essentially already answered the question. However I want to make some t$$anonymous$$ngs clear. It seems you have the wrong idea why you would use a delegate / event and what general approach is be$$anonymous$$nd an event. If the player provides an event that other objects can subscribe to, it means every subscriber gets the same event. It's literally like a real world subscription. Say you have subscribed for a certain magazine. Every month when a new edition is published, every subscriber gets the same t$$anonymous$$ng. That's the point of such a subscription.


So in t$$anonymous$$s case the "event" is raised by the magazine company when they have a new edition and each of their subscibers gets the same t$$anonymous$$ng.


What you have described doesn't sound like an event that all enemies are interested in. So it makes no sense to raise such an event if not all enemies want to know about that event. The events you talked about are individual events and the only two parties involved are the player and one particular enemy. So as Llama_w_2Ls said, having the player inform the particular enemy about its fate directly is the only logical approach. T$$anonymous$$s would be similar to somet$$anonymous$$ng like the police is calling you about the progress of a case that you filed. It makes no sense that the police calls every person in town about your case.


One could t$$anonymous$$nk of a reverse event that the enemy may provide. So when the enemy dies, the enemy may have an event that the player can subscribe to. So when the enemy dies, the enemy is raising the event, informing anyone who wants to know about the death of t$$anonymous$$s particular enemy. Of course that means the player would have to subscibe manually to that event for each enemy individually. However since the death of an enemy is usually triggered by an action of the player, it makes more sense to use direct communication. So the player who knows who he is attacking can directly reach out to a certain script on that particular enemy and call a "Hit" or "AddDamage" method on the enemy. That method may return a bool value if the enemy died during that $$anonymous$$t / attack so the player could react to that. Though keep in mind to keep t$$anonymous$$ngs seperated. So anyt$$anonymous$$ng related to the death animation of the enemy should be handled by the enemy script inside the Hit / AddDamage method w$$anonymous$$ch is probably called from the player's Attack method. The enemy can return information to the caller, so the player knows if the enemy died or not.


The player script should not be responsible to tell the enemy to play the death animation. That should happen implicitly in the "Hit" method of the enemy once the HP drops to 0 (or whatever game mechanic you're using). That'S the main point of OOP. Object oriented just means you have objects w$$anonymous$$ch encapsulate state (variables) as well as behaviour (methods that describe that behaviour). OOP is about constructing meaningful abstract interactions between objects and keep the object relevant code inside that object.


A common approach for many games is, that each enemy may have an OnDeath event w$$anonymous$$ch it raises when the enemy dies. Since the enemy is usually created by some sort of game manager, that manager could subscribe a callback to each enemy when it creates the enemy. That way the manager gets informed when an enemy dies. Note that such an event could have an argument of type Enemy. That way the dieing enemy could pass a reference to itself (`t$$anonymous$$s`) so the manager actually knows who raised the event. The manager may wants to know about the death for statistical purposes or game mechanic purposes. So when an enemy dies it may increase the kill count and maybe spawn a new / the next enemy as a result. There are many possible usecases.

Comment
ravengamed
Llama_w_2Ls

People who like this

2 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 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

Related Questions

Calling a method using delegate makes my object null 0 Answers

Delegates not working on iOS 0 Answers

How to make an Input system using delegates? 0 Answers

NGUI UICenterOnChild, find out what is centered 1 Answer

How to unsubscribe from InputSystem event properly 0 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