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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved No overload for 'method' matches delegate 'EventHandler'

Discussion in 'Scripting' started by rom10bvt, Apr 28, 2022.

  1. rom10bvt

    rom10bvt

    Joined:
    Apr 25, 2022
    Posts:
    2
    Hi,

    I spent some time tonight learning about delegates and events, following a similar example as in this video.



    I created two classes, one subscriber, one publisher.
    I wanted the subscriber to display the number of space counts.

    In the video I linked, at [10:10] there seems to be no issue for him to use "TestingEvents.OnspacePressedEventArgs' in the definition of his function passed to the delegate, instead of "EventArgs".

    Can someone explain me why?

    Here is the error, in the subscriber script, line 14:
    No overload for 'DisplaySpaceCounter' matches delegate 'EventHandler'

    Here is my code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class TestinEventsSubscriber : MonoBehaviour
    8. {
    9.     TestinEventsPublisher myPublisher;
    10.  
    11.     void Awake()
    12.     {
    13.          myPublisher = GetComponent<TestinEventsPublisher>();
    14.          myPublisher.OnSpacePressed += DisplaySpaceCounter;
    15.     }
    16.  
    17.     private void DisplaySpaceCounter(object sender, TestinEventsPublisher.OnSpacePressedEventArgs e)
    18.     {
    19.         Debug.Log("Space: " + e.spaceCount);
    20.     }
    21.  
    22. }
    23.  
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class TestinEventsPublisher : MonoBehaviour
    8. {
    9.     [SerializeField] public InputAction spaceBar;
    10.     public event EventHandler OnSpacePressed;
    11.     int spaceCount = 0;
    12.  
    13.     public class OnSpacePressedEventArgs : EventArgs
    14.     {
    15.         public int spaceCount;
    16.     }
    17.  
    18.     void Awake()
    19.     {
    20.         spaceBar.performed += InvokeOnSpaceBarPressed;
    21.     }
    22.  
    23.     void OnEnable()
    24.     {
    25.         spaceBar.Enable();
    26.     }
    27.  
    28.     void InvokeOnSpaceBarPressed(InputAction.CallbackContext context)
    29.     {
    30.         spaceCount++;
    31.         OnSpacePressed?.Invoke(this, new OnSpacePressedEventArgs{spaceCount = spaceCount});
    32.     }
    33. }
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    If you look at 09:20 in the video you will see that you have missed adding your OnSpacePressedEventArgs class as the generic parameter to your
    public event EventHandler OnSpacePressed;
    line.
    CM has it as:
    public event EventHandler<OnSpacePressedEventArgs> OnSpacePressed;
     
    rom10bvt and Bunny83 like this.
  3. rom10bvt

    rom10bvt

    Joined:
    Apr 25, 2022
    Posts:
    2
    Indeed, thank you so much!!!