1
\$\begingroup\$

This question already has an answer here:

I tried to make a game where you can destroy something when you swipe or tap. However, it says NullReferenceException & while the swipe control actually works, the gameobject can't be destroyed.

public class checkUp : MonoBehaviour {
private Swipe swipe;
private UIManager uiManager;

private void Awake()
{
    swipe = GetComponent<Swipe>();
    uiManager = GetComponent<UIManager>();
}



// Update is called once per frame
void Update()
{
    if (swipe.SwipeUp ) //NullReferenceException: Object reference not set to an instance of an object
    {
        Debug.Log("Passed!");
        uiManager.destroyed = true;
        Destroy(gameObject);
    }

Here's the partial code from swipe:

    if (swipeDelta.magnitude > 125)
    {
        //which direction?
        float x = swipeDelta.x;
        float y = swipeDelta.y;
        if (Mathf.Abs(x) > Mathf.Abs(y))
        {
            //left or right
            if (x < 0)
                swipeLeft = true;
            else
                swipeRight = true;
        }
        else
        {
            // Up or down?
            if (y < 0)
                swipeDown = true;
            else
                swipeUp = true;
        }
    }
\$\endgroup\$

marked as duplicate by DMGregory unity Mar 13 at 11:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ Does your GameObject have a Swipe component? \$\endgroup\$ – Eldy Mar 12 at 13:10
  • \$\begingroup\$ nope, tried to added it, and it works... \$\endgroup\$ – Chan Ming Mar 12 at 18:02
  • \$\begingroup\$ I'll post this as an answer then. \$\endgroup\$ – Eldy Mar 13 at 8:55
0
\$\begingroup\$

It is probable that your GameObject does not have a Swipe component. Try adding it and it should work.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.