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 /
avatar image
1
Question by laurG · Sep 13, 2017 at 05:13 PM · physicsperformancegarbage

UnityEvent.Invoke Generates Garbage?

Hi!

I created a simple Trigger component:

 public class Trigger : MonoBehaviour {
 
     public enum DisableMode
     {
         None,
         Enter,
         Exit
     }
     public DisableMode disableMode;
 
 
     [System.Serializable]
     public class TriggerEvent : UnityEvent { }
 
     public TriggerEvent onTriggerEnter;
     public TriggerEvent onTriggerExit;
 
     // privates
     private Collider m_collider;
     private bool m_triggered;
 
     void Awake()
     {
         m_collider = GetComponentInChildren<Collider>();
     }
 
     void OnEnable()
     {
         m_collider.enabled = true;
         m_triggered = false;
     }
 
     public void Nothing()
     {
 
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (m_triggered)
             return;
 
         m_triggered = true;
         onTriggerEnter.Invoke();
         
         if (disableMode == DisableMode.Enter)
             m_collider.enabled = false;
     }
 
     void OnTriggerExit(Collider other)
     {
         onTriggerExit.Invoke();
 
         if (disableMode == DisableMode.Exit)
             m_collider.enabled = false;
     }
 
 }

I created the Nothing method for demonstration purpose and assigned it to onTriggerEnter event: alt text

As you can see from this screenshot, Invoking an event with an empty action will generate 0.6KB garbage. Is there something I'm doing wrong? I'm sure this shouldn't happen.

alt text

Thank you!

setup.png (13.5 kB)
profiler.png (87.8 kB)
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 nt314p · Sep 13, 2017 at 11:40 PM 0
Share
  1. $$anonymous$$B is not that much garbage unless you are planning on calling this a lot (and besides there are other functions that create relatively more garbage than this).

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kaplica · Sep 13, 2017 at 08:35 PM

If you are using less than 2 listeners use C sharp events and if more then unity events. Unityevents creates garbage when dispatched (the first time) where C# events do not.

C sharp events tend to be faster than unitys anyway.

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 laurG · Sep 14, 2017 at 03:22 PM 0
Share

Hi @kaplica

Thank you for your answer!

I have 2 questions about it: 1. Is there any way to expose a C# event in the inspector? (This is almost mandatory for what I'm trying to achieve with the Trigger component.) 2. Could you clarify what you are referring to with If you are using less than 2 listeners use C sharp events and if more then unity events? Less than 2 listeners in the whole scene? Or on the same object? I'm placing the Trigger component on stuff the player can collect in a platformer game.

avatar image
0

Answer by _watcher_ · Mar 24, 2018 at 11:03 PM

I would like to know the same thing.

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

132 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 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

Add 2d rigidbodies to moving objects to increase performance or not? 1 Answer

Detecting Input on ~100 Objects 2 Answers

Fastest primitive collider 1 Answer

Should GetComponent() generate garbage when the component is not found? 1 Answer

Compare modern UI vs Text Mesh Mobile Performance 0 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