I have an update function of a gameobject where it listens for inputs
//InputManager.cs
public delegate void InputGiven(string input);
public static event InputGiven OnInputGiven;
void Update()
{
if(Input.GetButtonDown("X"))
{
OnInputGiven("X");
}
}
------------- Another file ---------------
//FileA.cs
private int count = 0;
public void Init()
{
InputManager.OnInputGiven += InvokedInput;
}
public void InvokedInput(string input)
{
++count;
}
(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).
Edit:
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).