• 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 May 27, 2018 at 02:42 AM by sean244 for the following reason:

Other

avatar image
Question by sean244 · May 26, 2018 at 08:51 PM · monobehavioureventsdelegates

Can a function derived from MonoBehavior subscribe to a static delegate?

If I declare a delegate like

  public static event Action <GameObject> OnChange;

Can Destroy(GameObject) subscribe to it, like

  OnChange += Destroy;

The reason that it's static is so that it can easily be accessed from other scripts.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Tobychappell · May 26, 2018 at 09:07 PM

Yes but all subscriptions to the event will try to destroy the one gameobject that is passed into it.

An alternative is to have a parameter-less Action event and encapsulate the Destroy(this.gameobject) in a lambda expression


     using System;
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     public class MyComponent : MonoBehaviour
     {
       public static event Action OnChange;

       public static void CallOnChange()
       {
         if (OnChange != null)
         {
           OnChange();
         }
       }

       private void Awake()
       {
         OnChange += () => Destroy(gameObject);
       }

       float timer = 3;

       private void Update()
       {
         timer -= Time.deltaTime;

         if (timer <= 0)
         {
           OnChange();
         }
       }
     }

     public class OtherClass
     {
       public void SomeMethod()
       {
         // Below raises compiler error
         MyComponent.OnChange();

         // Use a static relay to call the event, although I think this goes against how C# was designed but hey ho.
         MyComponent.CallOnChange();
       }
     }
Comment

People who like this

0 Show 8 · 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 sean244 · May 26, 2018 at 09:11 PM 0
Share

Sorry, I should have asked if this can be done when the event is static. I edited my question to reflect that.

avatar image Tobychappell sean244 · May 26, 2018 at 09:21 PM 0
Share

Yeah static is fine. Just tested.

avatar image Tobychappell sean244 · May 26, 2018 at 09:24 PM 0
Share

FYI, events can only be called inside the class its declared in.

avatar image sean244 Tobychappell · May 26, 2018 at 09:25 PM 0
Share

So it will only delete the gameobject that's actually invoking the method, not the one that's subscribing to it?

Show more comments

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

86 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

Related Questions

Newbie State Machine Behaviours with Events and Delegates Questions 1 Answer

Is there anyway to consume events so OnMouse events don't fire on objects with lower Z? 1 Answer

Call AddListener on sibling components from MonoBehaviour.Reset 0 Answers

Fire C# Event 3 Answers

NullReferenceException when call an event 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