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 /
avatar image
0
Question by Nanako · May 15, 2015 at 04:16 AM · eventlistener

How do i declare a non-static event?

I've asked (almost) this question before: http://answers.unity3d.com/questions/950815/how-do-i-declare-an-event-listener.html

I got some answers, the first one was to just make it static, which i went with and which worked up until now. Now i have a specific need for a per-instance event.

I got another answer there, from CHPedersen which explains how to do this non-statically, but frankly i just don't understand it. I cannot get it working. So can someone please explain this to me again and/or in more detail.

Here is what i have so far:

Declaring the event

     public delegate void DestinationEventHandler();
     public event DestinationEventHandler DestinationChanged;

Creating a listener to the event, using a reference (creature) to an instance of the class which contains the above code.

         creature.DestinationChanged += new creature.DestinationEventHandler(ReadPath);


This second part does not compile. I don't understand why. The example in the linked page, the only difference i could understand was the inclusion of "EventArgs e" in the handler. I don't know what that is, and it won't compile if i try to use it. Is that necessary?

One other difference i noticed is: "buffer_OnDataProcessed;" in the example, i don't understand the use of an underscore there, and attempting to do the same for me doesn't compile either.

I'm stuck, pls help ;-;

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 Baste · May 15, 2015 at 10:14 AM

I'm not quite sure what you're trying to do with "new creature.DestinationEventHandler(ReadPath);"

If you want to add the ReadPath method to the creature's DestinationChanged callback, you do it like this:

 creature.DestinationChanged += creature.ReadPath;

I find that using the classes Action and Func is more readable than delegates. They're predefined delegates that you can use - Action has no return values, while Func has one. In your case, you could replace this:

 public delegate void DestinationEventHandler();
 public event DestinationEventHandler DestinationChanged;

with this:

 public Action DestinationChanged;
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

20 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

Related Questions

Notifying multiple enemies on player death 0 Answers

GameObject.Find breaks code with UnityEvent 2 Answers

Event Listener for Javascript? 0 Answers

How do i declare an event listener? 2 Answers

Passing sender object as an argument in AddListener method 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