Event being sent twice

I have an update function of a gameobject where it listens for inputs


public delegate void InputGiven(string input);
public static event InputGiven OnInputGiven;

void Update()
    ------------- Another file ---------------

     private int count = 0;

     public void Init()
           InputManager.OnInputGiven += InvokedInput;

     public void InvokedInput(string input) 

(this doesnt represent my code, I just written this down to provide a better explanation)

So when I press ‘X’ for the first time, it comes into this function and count is incremented ( count == 1). However, I have a break point on the line ‘++count;’ and it comes into the same function a second time. So in the end, count is 2. Is there a way to prevent the event being sent out once? Or will I need to have a local variable that keeps track of when the function is called the 2nd time (maybe in the first time it comes into the function, we can set a bool to true, and the 2nd time it comes into this function, we’ll check if this bool is true. If it is, set it to false, and return without incrementing count).

Not sure if this information would evenmatter, but my InputManager.cs is a singleton class, where the class is called InputManager. And both files, InputManager.cs and FileA.cs are included only once on a game object (and both classes inherit MonoBehaviour).

Is your GetButtonDown code being called from OnGUI? OnGUI can be called multiple times during a frame to handle layout and other events. All of your input functions should be called from Update.

How is InvokedInput called? Is it a delegate? You’ve possibly assigned the delegate twice.

Other than those guesses, you aren’t providing enough context to really help further. Show us more of the code.

It sounds like you might be subscribing to the event twice, thus every time you press the key and raise the event InvokedInput() is called twice. Check if this is the case by putting a print statement in your Init() method.

If that’s the case, you’re actual bug is that Init is being called twice and you can work from there.