Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jun 10, 2013 at 07:39 PM by Jessy for the following reason:

Too subjective and argumentative

avatar image
0
Question by sportsfan78 · Jun 07, 2013 at 01:49 PM · collisioneventsdelegates

Delegate or Event: Reacting to another object's collision functions

Hey, folks!

I'm new to the fancier aspects of C#. I'm trying to figure out when to use Events instead of delegates.

I have a character who swings around a sword with a hitbox attached to it. When the hitbox collides with something, I want the character class to do the damage computations.

I currently have this working by firing off a delegate from the weapon-hitbox's OnTriggerEnter function. When the character class wakes up, they just add a function to the hitbox's trigger-enter delegate.

I haven't worked much with events. Would I have been better off with events?


The hitbox currently has the following code:

  public delegate void OnTriggerEnterDelegate(Collider Other);
  public OnTriggerEnterDelegate OnTriggerEnterSteps;
      
  void OnTriggerEnter(Collider Other){        
        if(OnTriggerEnterSteps!=null){
              OnTriggerEnterSteps(Other);    
        }
  }


The character code then assigns the damage function like so:

 WeaponHitBox.OnTriggerEnterSteps+=MeleeStrikeTarget;
Comment
Add comment · 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 Owen-Reynolds · Jun 07, 2013 at 03:08 PM 0
Share

How would you even add it as an event? A delegate is just a fancy way for you to call a function to be named later. But I$$anonymous$$HO events need to be created and dispatched by the engine. Since Unity doesn't have an OnOtherColliderEnter(Collider watchThis, Collision col), adding a new event seems impossible.

The way you have it seems to fake watching someone else's collision in the only way possible.

avatar image Jessy · Jun 07, 2013 at 04:04 PM 0
Share

"I want the character class to do the damage computations." doesn't sound like a good idea to me. Send whatever data you need, via an event, to whatever needs to react.

avatar image sportsfan78 · Jun 07, 2013 at 04:56 PM 0
Share

Thanks @Owen Reynolds and @Jessy. I'll go research events a little harder to figure out where they are best used.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Jessy · Jun 07, 2013 at 04:09 PM

You don't need to define your own delegate for this. C#'s System namespace already has an appropriate one created for you: Action. You're probably never going to have to define your own delegates.

Do not use PascalCase for fields. PascalCase would be used for properties, or events, which have similarities to properties behind the scenes. Do not use public fields. There is generally no reason to have public delegates; definitely use events for better encapsulation.

Your system doesn't sound great, to me, but given what you have, here's what you'd want to change to:

 public event Action<Collider> OnTriggerEnterSteps;

The rest of the code could stay the same.

Comment
Add comment · Show 5 · 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 sportsfan78 · Jun 07, 2013 at 04:57 PM 0
Share

Thanks again, @Jessy. I'll keep those tips in $$anonymous$$d.

avatar image Jessy · Jun 07, 2013 at 05:29 PM 0
Share

This doesn't seem like the appropriate site for this; I don't see a clear question that can be answered. You should delete this and take it to the forum after reading Stack Overflow for a few hours. Posts on that site from years ago generally get me up to speed on whatever program$$anonymous$$g topics I'm learning.

avatar image sportsfan78 · Jun 07, 2013 at 05:52 PM 0
Share

The answerable question was "Would I have been better off with events?", which at the time did not seem like a particularly helpful summary.

The expected result was that someone would say something along the lines of "no, events should only be used for UI buttons or [situation x] in unity" or perhaps "Oh, you can already just use 'OnTriggerEnter' as [Unity Concept Y]".

Your suggestion to send an event was actually even better, because it pointed out a better way to implement the problem.

Would a question of "How do I implement a sword impact?" be more appropriate? I thought that putting some effort into it first might make it easier on folks answering the questions.

I'll go check out the forums.

avatar image sportsfan78 · Jun 07, 2013 at 05:57 PM 0
Share

Also, the delete button is not working because there are some answers that cannot be deleted. What is customary in this situation?

avatar image Jessy · Jun 07, 2013 at 08:22 PM 0
Share

Can you close it? I can, if that's what you think we should do. I feel like this is something that will lead to an open-ended journey.

avatar image
0

Answer by sportsfan78 · Jun 08, 2013 at 07:30 AM

If I understand this page correctly, I don't have enough rep to close my own question. http://answers.unity3d.com/page/faq.html

Please close it if you get the chance.

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

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Method doesn't get implemented after subscribing to Func 1 Answer

Is there any event or delegate that is called with OnValidate() ? 0 Answers

Unity Events - Subscribe and Unsubscribe Odd Behavor 0 Answers

Help to understand events and delegates (Game Manager) 2 Answers

Using Delegates for separating UI and Logic, are these in the right place? 1 Answer


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