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

Can't have 2 scripts invoked by the same C# event ?

Discussion in 'Scripting' started by HiddenSide, Dec 20, 2020.

  1. HiddenSide

    HiddenSide

    Joined:
    Jul 17, 2018
    Posts:
    3
    Hello, I'm quite new to Unity & C#, and I encouter a bizarre issue when trying to add two different listeners (2 different scripts) to the same event.

    The event declaration and invocation :

    Code (CSharp):
    1.     public static AllBallsCtrlr Instance { get => instance; private set => instance = value; }
    2.  
    3.     public event EventHandler OnNoFreeBalls;
    4.  
    5. ...
    6.  
    7. OnNoFreeBalls?.Invoke(this, EventArgs.Empty);
    8.  
    9.  

    The first class that listen :
    Code (CSharp):
    1. public class MancheLogic : MonoBehaviour
    2. {
    3.  
    4. ...
    5.     void Start()
    6.     {
    7.         AllBallsCtrlr.Instance.OnNoFreeBalls += Respond_NoFreeBalls;
    8.  
    9.     }

    The 2nd class :
    Code (CSharp):
    1. public class ToplanesCtrlr : LanesCtrlr
    2. {
    3.  
    4. ...
    5.     protected override void Start()
    6.     {
    7.         base.Start();
    8.  
    9.         AllBallsCtrlr.Instance.OnNoFreeBalls += Respond_NoMoreFreeBalls;
    10.     }
    11.  

    No matter what, when the event is fired, only ONE class has it's method called (always MancheLogic.Respond_NoFreeBalls(...) )

    The only way for ToplanesCtrlr.Respond_NoMoreFreeBalls(...) to be called is if I comment (or erase) the line "AllBallsCtrlr.Instance.OnNoFreeBalls += Respond_NoFreeBalls;" in MancheLogic.

    I'm missing something but can't figure what ?!
    A lot of thx for any hlep with this.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    What you have should work just fine. The only thing that comes to mind for me is this:

    Do all of these objects exist in the scene from the start? Or is one or more of them instatiated?

    Where is AllBallsCtrlr.Instance assigned? Hopefully in Awake()?

    Where does the event get invoked? Is it being invoked from Start() perhaps?
     
    HiddenSide likes this.
  3. HiddenSide

    HiddenSide

    Joined:
    Jul 17, 2018
    Posts:
    3
    OK... I just found out what is happening ! :(

    It's really a silly error of mine : in the method "private void Respond_NoFreeBalls(object sender, EventArgs e)" of MancheLogic, I had a "throw new Exception();" that fired BEFORE the "OnNoFreeBalls?.Invoke(this, EventArgs.Empty);" on AllBallsCtrlr could reach the next objet, ToplanesCtrlr....

    And I tried to solve this for 3 hours (couldn't see my own error).
    Apologies for the time spent in trying to help me.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Ah yep, don't throw exceptions in event handlers! I should've mentioned that!
     
  5. HiddenSide

    HiddenSide

    Joined:
    Jul 17, 2018
    Posts:
    3
    Yeah ! Still learning, and very thankfull for your advices.