Home > Software engineering >  Why my event listener is not working in Unity using C#
Why my event listener is not working in Unity using C#

Time:06-07

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:

public class GameController : MonoBehaviour
{

public static Action isSinglePlayer = delegate { };

 void Start()
    {
        isSinglePlayer?.Invoke();
    }
}

So in the Bird Pigeon class I have the listener and the relevant code is like this :

public class BirdPigeon : MonoBehaviour
{
    public void Awake()
    {
        PV = GetComponent<PhotonView>();
        BattleRandomController.BirdDirectionTransfer =BirdDirectionMultiPlayer;
        GameController.isSinglePlayer  = SinglePlayer;
        BattleRandomController.isMultiPlayer =MultiPlayer;        
    }

 void Start()
    {     
       if (isMultiPlayerGame)
        {
            if (PV.IsMine)
                IndicatorForMe.SetActive(true);
            else
                IndicatorForMe.SetActive(false);
        }

     }

  public void SinglePlayer()
    {
        Debug.Log("isSinglePlayer function is executed................");
        isMultiPlayerGame = false;
        IndicatorForMe.SetActive(false);
    }
private void MultiPlayer()
    {
        Debug.Log("MultiPlayer function is executed");
        isMultiPlayerGame = true;
        IndicatorForMe.SetActive(true);
    }
}

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:

public class BattleRandomController : MonoBehaviour
{
 public static Action isMultiPlayer=delegate{};
 void Start()
    {
       isMultiPlayer?.Invoke();
     }
}

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 .

CodePudding user response:

The correct way to declare an event would be like

public event EventHandler MyEvent;

and raise it like

MyEvent?.Invoke(this, EventArgs.Empty)

You could use an arbitrary delegate in place of EventHandler or EventHandler<T>, but the most code I have seen uses EventHandler for events.

Note that you are using a static event, and there are several issues with such events. It may make thread safety more difficult, and can easily create memory leaks if you are not very through with unsubscribing. The singleton pattern might be an improvement, but I would recommend such singletons to only be global within some "context", and not for the entire application, that can make it easier to cleanly dispose of that context.

CodePudding user response:

maybe you should try to replace your action by this :

public class GameController : MonoBehaviour
{

public event Action isSinglePlayer;

 void Start()
    {
        isSinglePlayer?.Invoke();
    }
}
  • Related