Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Tanatosx3 · Jan 16, 2016 at 06:47 PM · c#eventevent triggeringevent-listener

Reverse Unity Event possible ?

Hey

I want to send an event when a game object gets destroyed when clicked, but i dont want to have one event for every game object because there will be like 50 of them :(

How can i make like a "reverse Unity Event" were i can listen on my game manger and send events when its get destroyed on OnDestroyed() ?

Comment
Add comment · Show 2
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 Hellium · Jan 15, 2016 at 09:36 AM 0
Share

An "easy" way to do what you want, is to define an abstract class, heriting from $$anonymous$$onoBehaviour. Then, all your objects heriting from $$anonymous$$onoBehaviour, now herits from this custom class

Then , define delegate + event, and define the OnDestroy function

 private void OnDestroy()
 {
     if( destroyEvent != null )
         destroyEvent() ;
 }

avatar image hexagonius Hellium · Jan 17, 2016 at 06:54 PM 0
Share

I'd make it a static event in that class. I'd also add a parameter "GameObject", so you can tell which of the calling objects is the one that fired it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Jan 17, 2016 at 11:46 PM

You can do this but I think you'll have to implement it in a custom class. By using delegates that send your GameObject as the parameter you can tell the EventManager what GameObject reference to send on to your handlers. Because the GameObject isn't actually deleted until the end of the frame you can do what you need to in those methods. If you don't mind a block of code, this is what a basic event handler with a parameter might look like:

 using System;
 using System.Collections.Generic;
 
 public enum EVENT { OnDeath, MyEvent2 }; // ... Other events
 public static class EventManager
 {
     // Stores the delegates that get called when an event is fired
     private static Dictionary<EVENT, Delegate> eventTable
                  = new Dictionary<EVENT, Delegate>();

     // Adds a delegate to get called for a specific event
     // The parameter type of the method added has to be specified
     public static void AddHandler<T>(EVENT evnt, Action<T> action)
     {
         if (!eventTable.ContainsKey(evnt)) eventTable[evnt] = action;
         else eventTable[evnt] = (Action<T>)eventTable[evnt] + action;
     }

     // Fires the event with the included parameter
     public static void Broadcast<T>(EVENT evnt, T param)
     {
         Delegate d;
         if (eventTable.TryGetValue(evnt, out d))
         {
             Action<T> action = d as Action<T>;
             if (action != null) action(param);
         }
     }
 }

That will let you both add handlers for your events and then broadcast them, in your case from the OnDestroy of the GameObject. The Action type, if you haven't seen it, is just a modern delegate that doesn't require the declaration. The 'T' parameter is a generic and kind of a variable that lets you put in any type. This is how you would use the EventManager above:

 using UnityEngine;
 
 public class DyingObject: MonoBehaviour {
 
     void OnDestroy()
     {
         EventManager.Broadcast(EVENT.OnDeath, gameObject);
     }
 
 }


 using UnityEngine;
 
 public class ListeningClass: MonoBehaviour {
 
     void Start()
     {
         // This is the reference to whatever object is going to be dying
         GameObject myDyingGO = GameObject.Find("yourDyingGameObject");
 
         // Adds a lambda expression, a generic method, that calls your method
         EventManager.AddHandler<GameObject>(EVENT.OnDeath, 
                                 (x) => DoSomethingOnDeath(myDyingGO));
         
         Destroy(myDyingGO);
         // Output: "Foo"
     }
 
     private void DoSomethingOnDeath(GameObject go)
     {
         // You can call this method for all of your dying GameObjects
         if (go.name == "yourDyingGameObject")
             Debug.Log("Foo");
     }
 
 }

Because you're using generics here you can also use this EventManager for any other type: ints, floats, custom classes. This gives you a lot more flexibility when it comes to cross-class communication. If I understood your question right I think this should solve it, if not let me know.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set up multiple Delegate overrides ? 1 Answer

How to create an event trigger script for intercepted events 0 Answers

Is it OK to create lots of event listeners? 0 Answers

How to direct scroll input to ScrollView? 0 Answers

UnityEvent not showing extension methods 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