I’ve a problem with this code. I always end up with null reference and can’t find a solution
using UnityEngine;
using System.Collections;
public class MyTapHandler : MonoBehaviour {
public GameObject tapper;
// reference to our tap gesture recognizer object
public TapGestureRecognizer tapRecognizer;
void Start()
{
// register to the tap recognizer's OnTap event
tapRecognizer.OnTap += tapRecognizer_OnTap;
}
// our tap event handler method
void tapRecognizer_OnTap( TapGestureRecognizer source )
{
GameObject obj = PickObject( source.Position );
if( obj.GetInstanceID() == tapper.GetInstanceID()){
Debug.Log( "You tapped ");
}else{
Debug.Log( “You didn’t pressed the right object” );}
}
GameObject PickObject (Vector2 screenPos){
Ray ray = Camera.main.ScreenPointToRay( screenPos );
RaycastHit hit = new RaycastHit();
if ( Physics.Raycast( ray, out hit ) ) {
return hit.collider.gameObject;
} else{
return null;
Debug.Log( "You tapped ");
else
Debug.Log( "You didn't pressed the right object" );
}
GameObject PickObject (Vector2 screenPos){
Ray ray = Camera.main.ScreenPointToRay( screenPos );
RaycastHit hit = new RaycastHit();
if ( Physics.Raycast( ray, out hit ) )
return hit.collider.gameObject;
else
return null;
}
}
And here the console error:
NullReferenceException: Object reference not set to an instance of an object
MyTapHandler.tapRecognizer_OnTap (.TapGestureRecognizer source) (at Assets/Scripts/MyTapHandler.cs:24)
TapGestureRecognizer.RaiseOnTap () (at Assets/Plugins/FingerGestures/Components/TapGestureRecognizer.cs:177)
TapGestureRecognizer.OnActive (IFingerList touches) (at Assets/Plugins/FingerGestures/Components/TapGestureRecognizer.cs:133)
GestureRecognizer.OnUpdate (IFingerList touches) (at Assets/Plugins/FingerGestures/Components/Base/GestureRecognizer.cs:181)
FGComponent.FingerGestures_OnFingersUpdated () (at Assets/Plugins/FingerGestures/Components/Base/FGComponent.cs:34)
FingerGestures.Update () (at Assets/Plugins/FingerGestures/FingerGestures.cs:973)