Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Utilizing UnityEvents effectively

Discussion in 'Getting Started' started by Green11001, Sep 26, 2021.

  1. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Hello! I recently learned about UnityEvents, and they seemed quite useful, so I was wondering the best ways to use them.
    I was thinking of just straight up splitting most scripts into separate parts which would all be joined via the UnityEvents. For instance, there would be a player shoot script which invokes a spawn script (and other scripts that would be needed) that instantiates a bullet, in hopes that later on I will be able to reuse different behaviors to create different weapons.
    Is this an effective usage of UnityEvents? Or are there issues? I don't have any experience or knowledge about using these (although they seem fairly straightforward), and it would be quite troubling if I built my project on them only to realize later on that there are too many issues for it to function properly.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, you have it exactly right. If a script does something that other scripts may need to know about, expose it via a UnityEvent. Conversely, if a script can do something that you might want triggered by something else, then expose that as an event-compatible public method. Then just hook those things together like Lego blocks.

    I've been working this way for years, and have never had any regrets.
     
  3. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    I see, thanks!