How to use Unity Events and Unity Actions [Subs&UnSubs]
My main problem is when Vuforia events triggered like image targeting lost everthing works correctly inputs disconnected but when I re-found image target with camera and use jump button its triggering twice.
How should I use UnityActions, UnityEvents correctly for these kind of situations ?
Vuforia Observer Handler
public class DefaultObserverEventHandler : MonoBehaviour
{
public UnityEvent OnTargetFound;
public UnityEvent OnTargetLost;
protected virtual void OnTrackingFound()
{
OnTargetFound?.Invoke();
}
protected virtual void OnTrackingLost()
{
OnTargetLost?.Invoke();
}
}
AR MANAGER
public class ARManager : MonoBehaviour
{
public UnityAction _onFound;
public UnityAction _onLost;
private void Awake()
{
_onFound += GameManager.Instance._inputManager.ConnectInputs;
_onLost += GameManager.Instance._inputManager.DisconnectInputs;
_observerEventHandler.OnTargetFound.AddListener(_onFound);
_observerEventHandler.OnTargetLost.AddListener(_onLost);
}
}
Input Manager
public class InputManager : MonoBehaviour
{
public UnityAction OnJumpPressedAction;
public void ConnectInputs()
{
OnJumpPressedAction +=
GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterMovementSystem>().OnJump;
GameManager.Instance._guiManager._jumpButton.onClick.AddListener(OnJumpPressedAction);
}
public void DisconnectInputs()
{
OnJumpPressedAction -=
GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterMovementSystem>().OnJump;
GameManager.Instance._guiManager._jumpButton.onClick.RemoveListener(OnJumpPressedAction);
}
}
Thanks !
I didn't read the code too carefully, I just reacted to you symptom "jump button its triggering twice"..
Most cases this happens when, for some reason, the delegate is added twice to the event. So before adding specific delegate, always delete it to make sure it will not get added twice.
Check this first and if the issue persist, then debug further..
Your answer
![](https://koobas.hobune.stream/wayback/20220613050522im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I cant subscribe to my event manager ? What should I do? 1 Answer
Delegates & Events registration & deregistration - Doubt 0 Answers
Question regarding delegates and events 1 Answer
Instance variables and this == null in event handler 1 Answer
Argument Exception when no arguments? Method Arguments are Incompatible 0 Answers