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
1
Question by EvandroLins · Jan 28, 2013 at 05:28 PM · c#removedelegate

How to remove a method from a delegate

I have added one method into a delegate, but i'm unable to remove it.

The code I'm using:

 public delegate void     MyDelegate();
 static public MyDelegate delegateInstance;
     
 void Start () {
     delegateInstance = new MyDelegate(MyMethod);
     delegateInstance -= new MyDelegate(MyMethod);
 
     delegateInstance();
 }

I suppose it would not run the MyMethod, because it was removed before I call the delegate. Yet it ignores my try to remove it.

Comment
Add comment · Show 1
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 Dave-Carlile · Jan 28, 2013 at 06:12 PM 0
Share

Does $$anonymous$$y$$anonymous$$ethod happen to be JavaScript? Looks like there may be some issues with that: http://forum.unity3d.com/threads/30202-Removing-methods-C-delegates

Otherwise, your code works correctly for me.

3 Replies

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

Answer by Jeffom · Jan 28, 2013 at 06:41 PM

on your code you're not adding an event to be called by the delegate youre just setting the reference, that's why it won't work with the "-=" operator. try this(i'm not sure if it'll work):

 public delegate void MyDelegate();
 static public MyDelegate delegateInstance;
  
 void Start () {
 delegateInstance += new MyDelegate(MyMethod);
 delegateInstance -= new MyDelegate(MyMethod);
  
 delegateInstance();
 }



to just remove the delegate reference set it to null and do a null check

 delegateInstance = null
 if( delegateInsatance != null )
      delegateInstance();

for more info about events access http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx

Comment
Add comment · Show 1 · 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 EvandroLins · Jan 31, 2013 at 12:05 PM 0
Share

Thanks! \o/ It worked!

avatar image
2

Answer by MalikDrako · Jan 28, 2013 at 07:00 PM

You are trying to remove a different instance than you originally assigned, so it doesnt do anything. You dont need to create a new MyDelegate object when assigning, you can just give it a method that fits.

 public delegate void    MyDelegate();
 static public MyDelegate delegateInstance;
  
 void Start () {
     delegateInstance += MyMethod; // using += allows it to call multiple methods
     delegateInstance -= MyMethod;
  
     if (delegateInstance != null) // you should null-check delegates
         delegateInstance();
 }

Using += will allow you to do something like this:

 public delegate void    MyDelegate();
 static public MyDelegate delegateInstance;
  
 void Start () {
     delegateInstance += MyMethod;
     delegateInstance += MyMethod2;
     delegateInstance += MyMethod3;
     delegateInstance -= MyMethod;
  
     delegateInstance(); // will call MyMethod2 and MyMethod3 but not MyMethod
 }
Comment
Add comment · Show 1 · 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 EvandroLins · Jan 31, 2013 at 12:06 PM 0
Share

Thanks! :]

avatar image
0

Answer by ammarhassan48 · Apr 04, 2017 at 09:52 AM

Rather than using Remove() or RemoveAll() static methods of Delegate class , you can use -= operator which has been overloaded for the same methods.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Subscribing to Unity Events with different signatures 1 Answer

Removing GameObjects from list (with List.Remove()) doesn't seem to be removing them 2 Answers

Play an Animation through a Script 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