Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Callback gets called multiple times when it shouldn't. Is there are more elegant workaround?

Discussion in 'Input System' started by PeterPudeltreter, May 1, 2021.

  1. PeterPudeltreter

    PeterPudeltreter

    Joined:
    Apr 1, 2021
    Posts:
    9
    I'm using Unity 2020.3.3f1 with the new input system. I use a control scheme where I define the actions and on my character I use a Player Input Component where I invoke Unity Events. Those are handled in my movement script. I check for context.performed and handle my event.

    Everything works fine when I use the prefab directly but when I spawn the prefab though a Player Input Manager Component suddenly for every valid callback an invalid call gets made. A call from the prefab itself or somewhere else (it's not a valid scene object is all I found out). This results in all members not being initialized for that second call and thus spawning a swarm of NullpointerExeptions.

    The workaround I am using so far is to check for a valid scene object, and if that caller isn't part of a valid scene I immediately return before handling the event:
    Code (CSharp):
    1.     public void OnLook(InputAction.CallbackContext context)
    2.     {
    3.         // bug workaround: http://answers.unity.com/answers/1762099/view.html
    4.         if (!gameObject.scene.IsValid())
    5.         {
    6.             return;
    7.         }
    8.  
    9.         if (context.performed)
    10.         {
    11.             targetLookInput = context.ReadValue<Vector2>();
    12.         }
    13.     }
    14.  
    But I really don't like this approach and was wondering if there is a better way to avoid the exceptions? Can I avoid having to check this for every callback? Is this there a fix coming?
     
    rekreingd likes this.
  2. rekreingd

    rekreingd

    Joined:
    Apr 26, 2021
    Posts:
    1
    Just recently discovered this issue, your solution didn't solve the problem either :/
    Would love some info on either plans to fix it or better solutions.