Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Reid_Taylor · Aug 30, 2020 at 05:33 PM · inputremove

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
   }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Reid_Taylor · Aug 30, 2020 at 06:47 PM 0
Share

Will try! Thanks for the answer!

avatar image Reid_Taylor · Aug 30, 2020 at 06:57 PM 0
Share

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...:)

avatar image Reid_Taylor · Aug 30, 2020 at 07:24 PM 0
Share

Sorry... I got it working I accidentally added '()' After the Context Function when using action.performed += ToggleONOFFWithContext... It worked Thanks!

avatar image daniFMdev · Dec 07, 2021 at 11:10 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges