Question by
Reid_Taylor · Aug 30, 2020 at 06:04 AM ·
inputeventsremove
Help with New Input System: Removing Events via C#
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
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613010830im_/https://answers.unity.com/themes/thub/images/avi.jpg)