Search Unity

Whats the "proper way" to get a GameObject to fire off an Event.... from the outside of that object?

Discussion in 'Getting Started' started by Dirt-e, Feb 1, 2019.

  1. Dirt-e

    Dirt-e

    Joined:
    Jan 11, 2019
    Posts:
    6
    Hi there,...

    I've been running around in circles with this problem for days, and I suspect I have something to learn about "static". Maybe someone can point me in the right direction.

    I have these three Objects: Trigger, Emitter & Subscriber. I can make the Emitter fire-off an Event and the subscriber springs into action. Awesome!
    2 Kopie 4.png

    But every now and then, I need another object (Trigger) to kinda "punch" the Emitter to fire-off that event too. 2 Kopie.png

    I thought the most elegant way would be to just call the corresponding function, but I can't do that,... unless I make the function static:
    Bildschirmfoto 2019-02-01 um 10.35.22.png

    ...but then I can't fire off the Event anymore,... unless that's static too:
    Bildschirmfoto 2019-02-01 um 10.23.21.png

    So I made MyEvent static too and everything appeared fine,... but now I can't see the Event from the Inspector anymore...

    Bildschirmfoto 2019-02-01 um 10.30.05.png


    What am I missing? Do I really have to instantiate an Emitter object somewhere? And if so, where?
    I find the code to be much more elegant with the Emitter functions being static and me able to just call them through Emitter.RaiseEvent()

    Help much appreciated.
     
    Last edited: Feb 1, 2019
  2. Dirt-e

    Dirt-e

    Joined:
    Jan 11, 2019
    Posts:
    6
    Ahhhrghhh,..... I GOT IT!!!!!! I GOT IT!!!!!! (At least I think I got it)

    How could I've been so dumb?!?

    I know, I know, it's pretty silly to answer your own question on the forums, but it just occurred to me...

    The Trigger object needs a reference to the Emitter in order to call it's functions!
    Bildschirmfoto 2019-02-01 um 14.53.27 2.png

    and then the event gets fired twice :)
    Bildschirmfoto 2019-02-01 um 15.08.01.png

    Me: Thank you Dirt-e!!!
    Dirt-e: You're welcome, Me!
    :)))))))))))
     
    JeffDUnity3D and DerrickMoore like this.