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

Create my own public events

Discussion in 'Scripting' started by IMAN_SH, Jul 19, 2021.

  1. IMAN_SH

    IMAN_SH

    Joined:
    Mar 8, 2016
    Posts:
    16
    Hi.
    I want to know, can I create my own public events in unity?
    I mean events like Start(), OnEnable() and etc.

    And I create an event with the name OnFire() and I use it in another script directly? How?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    IMAN_SH likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Use it how in another script?

    If the method is public, and you know the component type, you can just call the method/function directly.
    Code (csharp):
    1. target.GetComponent<OnFireReceiver>().OnFire();
    If you don't know what components you'd be calling it on, and want to ambiguously call a funciton of some name on all components attached to a GameObject that have a method named that. You can use SendMessage:
    https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html
    (I personally don't like this method as it relies on string names, and could accidentally call a method on a script that just happened to be named the same but wasn't intended to be called)

    There's the ExecuteEvents system which allows doing so by some interface contract which avoids the string issues of SendMessage:
    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/MessagingSystem.html

    ...

    But yeah, I don't know what you necessarily mean by "use it in another script directly".

    Note that there's 2 general ways discussed so far here. In the messages approach (Start, OnEnable, SendMessage), this is a situation where the receiver doesn't necessarily subscribe with the dispatcher that it wants to receive the message. It just implements the message callback and it can receive that message arbitrarily.

    In an event system like C# events, or UnityEvents. The receiver has to explicitly subscribe with the dispatcher before it can receive the events.

    That ExecutEvents/MessagingSystem is more like messaging. In that again you don't have to subscribe with the dispatcher, but at least you have to obey some contract (in the form of the interface) before you can receive them.

    What sort of roles do you want to enforce between the sender/receiver?

    How do you want your event/messaging system to behave?

    You have lots of options.
     
    Last edited: Jul 19, 2021
    IMAN_SH and PraetorBlue like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Lord of Duct dances around the topic of an abstract "interface" here, but I want to come out and say explicitly:

    C# interfaces in Unity are awesome!!

    Here's some of my feverish scribblings about it, as they might be useful in your situation:

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
    IMAN_SH likes this.