Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can i subscribe to event System Click?

Discussion in 'UGUI & TextMesh Pro' started by resetme, Feb 7, 2016.

  1. resetme

    resetme

    Joined:
    Jun 27, 2012
    Posts:
    204
    Hi,

    If i have a canvas with a button that say "every body jump" and i want all my game objects that are subscribed to that click input to jump, how do i program it?

    I did it with a script that have a public function that call "JumpDelegate", so when i click the button i call that function, so all my scripts that are subscribed to the Delegate event listen to it. But i was thinking, what if i just implement something like: using UnityEngine.EventSystem on all my scripts and then check if the UI button is clicked, after that i check what button was clicked, if it was jump, then i jump. That would be better than creating my owns delegates right?

    but how?
     
  2. jinxed_byte

    jinxed_byte

    Joined:
    Mar 29, 2014
    Posts:
    17
  3. Fran.Fndz

    Fran.Fndz

    Joined:
    Jan 27, 2016
    Posts:
    2
    But for that i will have to reference the button in all my scripts, right?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    no, the whole point of the Event approach is that it is "loosely coupled". The button shouts without caring who is listening, the listeners listen for a shout without caring where it came from.