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

Why my event listener is not working in Unity using C#

Discussion in 'Editor & General Support' started by Chethan007, Jun 7, 2022.

  1. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    51
    So I am working on a game which has both single Player as well as multiplayer functionality in it. On one hand I have a bird script which should function differently on the basis as to whether it is a single player game or a mutli-player game. But I simply can't understand why my event listener is not working as such.

    So for the Single Player game I have a script called GameController attached to a gameObject called GameController. In the Start function of it I am firing an event saying that it is a single player. Below is the relevant code:

    Code (CSharp):
    1. public class GameController : MonoBehaviour
    2. {
    3.  
    4. public static Action isSinglePlayer = delegate { };
    5.  
    6. void Start()
    7.    {
    8.        isSinglePlayer?.Invoke();
    9.    }
    10. }
    So in the Bird Pigeon class I have the listener and the relevant code is like this :

    Code (CSharp):
    1. public class BirdPigeon : MonoBehaviour
    2. {
    3.    public void Awake()
    4.    {
    5.        PV = GetComponent<PhotonView>();
    6.        BattleRandomController.BirdDirectionTransfer+=BirdDirectionMultiPlayer;
    7.        GameController.isSinglePlayer += SinglePlayer;
    8.        BattleRandomController.isMultiPlayer+=MultiPlayer;      
    9.    }
    10.  
    11. void Start()
    12.    {    
    13.       if (isMultiPlayerGame)
    14.        {
    15.            if (PV.IsMine)
    16.                IndicatorForMe.SetActive(true);
    17.            else
    18.                IndicatorForMe.SetActive(false);
    19.        }
    20.  
    21.     }
    22.  
    23.   public void SinglePlayer()
    24.    {
    25.        Debug.Log("isSinglePlayer function is executed................");
    26.        isMultiPlayerGame = false;
    27.        IndicatorForMe.SetActive(false);
    28.    }
    29. private void MultiPlayer()
    30.    {
    31.        Debug.Log("MultiPlayer function is executed");
    32.        isMultiPlayerGame = true;
    33.        IndicatorForMe.SetActive(true);
    34.    }
    35. }
    But when I run the code the Debug.log statement "isSinglePlayer function is executed................" doesn't get executed at all.

    Similarly for the multiplayer game scene I have a gameObject called Battle Random controller attached to game object of the same name and in the Start function of that I am firing an event to indicate it is a multiplayer and still the above listener for that which is Multiplayer, the debug.log statement doesnt get executed only i.e.,"MultiPlayer function is executed"

    The Relevant code for the multiplayer scene is as follows:

    Code (CSharp):
    1. public class BattleRandomController : MonoBehaviour
    2. {
    3. public static Action isMultiPlayer=delegate{};
    4. void Start()
    5.    {
    6.       isMultiPlayer?.Invoke();
    7.     }
    8. }
    Earlier I had fired the event in the Awake and was listening in the Start of the bird pigeon class which somewhere I read was wrong. Now there is a listener ready in the awake function of the BirdPigeon class but I can't understand as to why the listener is not functioning .
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    You are invoking an Action with an empty body. Try it like this:

    Code (CSharp):
    1. public static Action isSinglePlayer = delegate { Debug.Log("Damn!"); };
     
  3. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    51
    Only that "Damn" stuff is getting printed
     
  4. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    69
    Are the GameController and BirdPigeon game objects in the same scene?
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    That "Damn!" was meant for you to realize that you've got to write the code that you wish to run in there. :D
     
  6. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    69
    They're adding delegates in BirdPigeon.Awake
     
  7. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    51
    yea indeed
     
  8. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    Hmm okay I missed that. But why does it log "Damn"? That means it still calls the delegate{} and doesn't replace it with an actual method.

    Try using = assign instead of +=
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,849
    The System.Action delegates doesn't need to be used with inline initialisers. If the callback is only being invoked in it's own class it can be made an event too:
    Code (CSharp):
    1. public static event Action IsSinglePlayer;
    Your logic seems a bit convoluted though. Having some sort of global boolean to say whether or not you're in single/multiplayer would be a lot more straight-forward.
     
    Vryken likes this.
  10. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    51
    Yea, I didnt have much time, so I used a PlayerPref and Enum to store whether it is single player or multiplayer when ever the respective buttons are selected