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

In doc, what mean "Adding multiple identical listeners results in only a single call being made." ?

Discussion in 'Scripting' started by anthonov, Nov 2, 2020.

  1. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    because when I add multiple identical listeners to an UnityEvent, it fire them all, not just one.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Maybe there's a documentation error. Make a tiny project that proves it and submit a bug.
     
  3. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    can you confirm I understood well the concept ?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4.  
    5. public class TestListener : MonoBehaviour
    6. {
    7.     public UnityEvent fire;
    8.  
    9.  
    10.     void Fire()
    11.     {
    12.         Debug.Log("fire");  // fired 3 times. Doc says : Adding multiple identical listeners results in only a single call being made.
    13.     }
    14.  
    15.     void OnEnable()
    16.     {
    17.         fire.AddListener(Fire);
    18.         fire.AddListener(Fire);
    19.         fire.AddListener(Fire);
    20.  
    21.         fire.Invoke();
    22.     }
    23.  
    24. }
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    If it’s a documentation error, there’s a higher chance they change the actual behavior to match the docs than actually changing the docs.
     
    PraetorBlue likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I am certainly not going to dig for that one magical piece of documentation you reference and yet don't give me a link to!
     
  6. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    If we understand "identical listeners," well, it certainly calls them twice to me.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class gurg : MonoBehaviour
    5. {
    6.     public Button button;
    7.  
    8.     void Start()
    9.     {
    10.         button.onClick.AddListener(ButtonWasPressed);
    11.         button.onClick.AddListener(ButtonWasPressed);
    12.     }
    13.  
    14.     void ButtonWasPressed()
    15.     {
    16.         Debug.Log("Woo");
    17.     }
    18. }
    Screen Shot 2020-11-02 at 12.52.54 PM.png
     
  8. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    filled report.
     
  9. RandomUnityDev123

    RandomUnityDev123

    Joined:
    Oct 28, 2022
    Posts:
    15
    What was the bug report you filed pls? When did they address it? Still seeing it in 2022.3
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Most likely this won't get fixed. It USED to be that you had to wrap it in your own delegate:

    btn.AddListener( delegate { MyFunc(); });


    Now you can just do:

    btn.AddListener( MyFunc);


    and pass in a
    System.Action


    Since it can now take a
    System.Action
    , I'm guessing their new overload simply wraps it in a brand-new fresh and shiny
    delegate{}
    and calls the original .AddListener()

    This means that each time you call it, while you may give it the same
    System.Action
    , the overload wraps it in a different
    delegate{}
    made up on the spot in the function overload.

    That means they are different delegates.