#Help

1 messages · Page 1 of 1 (latest)

wooden vigil
#

I need help cause this error shows up

#

My code:

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Swipe : MonoBehaviour
{
    private bool tap, swipeLeft, swipeRight;
    private bool isDraging = false;
    private Vector2 startTouch, swipeDelta;

    private void Update()
    {
        tap = swipeLeft = swipeRight = false;

        #region Mobile Inputs
        if (Input.touches.Length > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                isDraging = true;
                tap = true;
                startTouch = Input.touches[0].position;
                    }
            else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
            {
                isDraging = false;
                Reset();
            }
        }
        #endregion


        swipeDelta = Vector2.zero;
        if (isDraging)
        {
            if (Input.touches.Length > 0)
                swipeDelta = Input.touches[0].position - startTouch;
            
        }

        if (swipeDelta.magnitude > 125)
        {
            //which direction?
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                if (x < 0)
                    swipeLeft = true;
                else
                    swipeRight = true;
            }

            Reset();
        }

    }

    private void Reset()
    {
        startTouch = swipeDelta = Vector2.zero;
    }


    public bool swipeRight { get { return swipeRight; } }
    public Vector2 swipeDelta { get { return swipeDelta; } }
    public bool swipeLeft { get { return swipeLeft; } }
}


//public Vector2 swipeDelta { get { return swipeDelta; } } | public bool swipeLeft { get { return swipeLeft; } } | public bool swipeRight { get { return swipeRight; } }
devout relic
#

Your properties are named the same as your fields. That's not possible; you can't have two elements with the same name.
Also, properties (and public members in general) should be named in PascalCase, not camelCase.
Lastly though, you don't even need fields for the values you want to expose; Just make auto-properties and remove the corresponding fields altogether: cs public bool SwipeRight { get; private set; } public bool SwipeLeft { get; private set; } public Vector2 SwipeDelta { get; private set; }

#

And for the purpose of readability, you should put properties somewhere at the top with the fields, not at the very bottom.