Hi community,
I am trying to catch or prevent a MissingReferenceException after I destry a gameobject with OnContactStay. My rig is a custom Vive Rig with the standard Interaction Manager.
Here is my script:
public class CMD_Handinteractor : MonoBehaviour {
protected InteractionBehaviour _intObj;
protected CMD_Socket CMD_socket;
private int counter;
protected virtual void Start() {
counter = 0;
_intObj = GetComponent<InteractionBehaviour>();
CMD_socket = GetComponent<CMD_Socket> ();
_intObj.OnContactStay += OnContactStay_Kill;
_intObj.OnContactEnd += OnContactEnd;
}
private void OnContactStay_Kill(){
counter++;
Debug.Log (counter);
StartCoroutine(CMD_socket.SetSphereColor (2));
if (counter >= 60) {
_intObj.OnContactEnd -= OnContactEnd;
_intObj.OnContactStay -= OnContactStay_Kill;
Destroy (this.gameObject);
}
}
I am getting the Exception as soon as I destroy it. What am I missing? Tried to go down the stacktrace but I got stuck. Here it is:
MissingReferenceException: The object of type 'InteractionBehaviour' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Leap.Unity.Interaction.InteractionBehaviour.Leap.Unity.Interaction.IInteractionBehaviour.get_gameObject ()
Leap.Unity.Interaction.InteractionController.Leap.Unity.Interaction.IInternalInteractionController.CheckContactEnd (System.Collections.Generic.HashSet`1& contactEndedObjects) (at Assets/LeapMotion/Modules/InteractionEngine/Scripts/InteractionController.cs:1431)
Leap.Unity.Interaction.InteractionManager.<checkEndingContacts>m__2 (Leap.Unity.Interaction.InteractionController maybeEndedContactingController, System.Collections.Generic.HashSet`1& endContactedObjects) (at Assets/LeapMotion/Modules/InteractionEngine/Scripts/InteractionManager.cs:432)
Leap.Unity.Interaction.InteractionManager.remapMultiInteractionObjectStateChecks (ReadonlyHashSet`1 controllers, Leap.Unity.Interaction.MultiStateChangeCheckFunc multiObjectStateCheckFunc, System.Action`2 actionPerInteractionObject) (at Assets/LeapMotion/Modules/InteractionEngine/Scripts/InteractionManager.cs:601)
Leap.Unity.Interaction.InteractionManager.checkEndingContacts (ReadonlyHashSet`1 interactionControllers) (at Assets/LeapMotion/Modules/InteractionEngine/Scripts/InteractionManager.cs:429)
Leap.Unity.Interaction.InteractionManager.fixedUpdateInteractionControllers () (at Assets/LeapMotion/Modules/InteractionEngine/Scripts/InteractionManager.cs:394)
Leap.Unity.Interaction.InteractionManager.FixedUpdate () (at Assets/LeapMotion/Modules/InteractionEngine/Scripts/InteractionManager.cs:295)
Thanks all!