[SOLVED] Trouble passing Event info as a parameter.

I want to create events and then, functions which are subscribed to the event can access information about the event. For example, in Class 2 below, I want it to be able to access things such as touch.position, etc.

Class 1:

        public delegate void TouchEventHandler (object sender,EventArgs e);
        public event TouchEventHandler TouchBegan;
  public Vector2 touchPosition;
        void Update ()
        {
                if (Input.touchCount > 0) {
                        for (int i = 0; i < Input.touchCount; i++) {
                                Touch touch = Input.GetTouch (i);
                              
                                if (touch.phase == TouchPhase.Began) {
                                        TouchEventHandler touchBegan = TouchBegan;
                                        if (touchBegan != null)
                                                touchBegan (this, e);
                                }
}}

Class 2:

    void OnTouchBegan (EventArgs e)
    {
        Debug.Log ("Ran");
    }

The OnTouchBegan definition misses one parameter.

What do you mean?

Sorry, maybe I’m wrong, but I thought that “void OnTouchBegan (EventArgs e)” is the method you want to call with “touchBegan (this, e);”.

Why are you making it more difficult than it needs to be?

 public delegate void TouchEventHandler (Touch touchInfo);
  public event TouchEventHandler TouchBegan;
  public Vector2 touchPosition;
        void Update ()
        {
                if (Input.touchCount > 0) {
                        for (int i = 0; i < Input.touchCount; i++) {
                                Touch touch = Input.GetTouch (i);
                            
                                if (touch.phase == TouchPhase.Began) {
                                        TouchEventHandler touchBegan = TouchBegan;
                                        if (touchBegan != null)
                                                touchBegan (touch);
                                }
}}
void OnTouchBegan (Touch touchInfo)
    {
        Debug.Log ("Position " + touchInfo.position);
    }
1 Like

Thanks that solved it.