Removing Event from New Input System Not Working!
Hello, So I have a flashlight pickup that when picked up ENABLE() is called and when dropped DISABLE() is called. I have been having trouble finding a way to add function to the PlayerInput Component via C#... After doing some research I tried this:
public class Flashlight : MonoBehaviour, IHoldable
{
public GameObject spotLight;
public InputAction action;
private Player player;
public bool ENABLED = false;
public void ENABLE(Player _player)
{
ENABLED = true;
player = _player;
/// THIS WORKS
action = player.input.input.actions["RightTrigger"];
action.performed += ctx => ToggleONOFF();
/// THIS WORKS
}
public void DISABLE(Player player)
{
/// THIS DOESNT WORK
action = player.input.input.actions["RightTrigger"];
action.performed -= ctx => ToggleONOFF();
/// THIS DOESNT WORK
spotLight.SetActive(false);
ENABLED = false;
}
public void ToggleONOFF()
{
if (!ENABLED)
return;
spotLight.SetActive(!spotLight.activeInHierarchy);
}
}
So this made it so I could toggle flash light! But when I drop the flashlight and pickup it up again it toggles 2x on trigger... So why is this:
action = player.input.input.actions["RightTrigger"];
action.performed -= ctx => ToggleONOFF();
Not removing the event from the the action? Help much appreciated! Thanks
Answer by Hellium · Aug 30, 2020 at 06:15 PM
When calling action.performed -= ctx => ToggleONOFF();
, you are creating a new lambda function, thus, you are not removing the one you have added.
public void ENABLE(Player _player)
{
ENABLED = true;
player = _player;
/// THIS WORKS
action = player.input.input.actions["RightTrigger"];
action.performed += ToggleONOFFWithContext;
/// THIS WORKS
}
public void DISABLE(Player player)
{
action = player.input.input.actions["RightTrigger"];
action.performed -= ToggleONOFFWithContext;
spotLight.SetActive(false);
ENABLED = false;
}
private void ToggleONOFFWithContext(InputAction.CallbackContext context) // I don't know if it's the correct type
{
ToggleONOFF();
}
public void ToggleONOFF()
{
if (!ENABLED)
return;
spotLight.SetActive(!spotLight.activeInHierarchy);
}
So this: action.performed += ToggleOnOffWithContext();
and this: action.performed -= ToggleOnOffWithContext();
are both giving errors. The first saying that there is no argument for context(I don't have any context to pass through). The Second saying that "Cannot implicitly convert type void to System.Action.
Sorry if I'm missing something here I don't have much experience with events and delegates...:)
Sorry... I got it working I accidentally added '()' After the Context Function when using action.performed += ToggleONOFFWithContext
... It worked Thanks!
I'm facing this issue and I don't think your code is working as expected. The code is only working because of ENABLED
, but that's redundant because you're supposedly unsubscribing from the event.
Your answer
Follow this Question
Related Questions
Help with New Input System: Removing Events via C# 0 Answers
PointAndClick 1 Answer
How use ANDROID NAVIGATION BUTTONS 0 Answers
Touch Controls without virtual joystick 0 Answers
how can i touch the fast objects ? 1 Answer