Hi, I checked other threads but could not find an answer. I get this error when I load the same scene like with try again button. error shows 2 scripts that has the possible erron but I cant figure it out please help
public class movement : MonoBehaviour
{
Rigidbody rigid;
private float zOffset = 20;
private void Start()
{
rigid = GetComponent<Rigidbody>();
SwipeDetector.OnSwipe += SwipeDetector_OnSwipe;
}
private void SwipeDetector_OnSwipe(SwipeData data)
{
Vector3[] positions = new Vector3[2];
positions[0] = Camera.main.ScreenToWorldPoint(new Vector3(data.StartPosition.x, 0, data.StartPosition.y));
positions[1] = Camera.main.ScreenToWorldPoint(new Vector3(data.EndPosition.x, 0, data.EndPosition.y));
Vector3 throwdir = (positions[1] - positions[0]).normalized;
throwdir.y = 0;
rigid.AddForce(-throwdir*750f,ForceMode.Acceleration); //error line
}
}
and this
public class SwipeDetector : MonoBehaviour
{
private Vector2 fingerDownPosition;
private Vector2 fingerUpPosition;
[SerializeField]
private bool detectSwipeOnlyAfterRelease = false;
[SerializeField]
private float minDistanceForSwipe = 20f;
public static event Action<SwipeData> OnSwipe = delegate { };
private void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerUpPosition = touch.position;
fingerDownPosition = touch.position;
}
if (!detectSwipeOnlyAfterRelease && touch.phase == TouchPhase.Moved)
{
fingerDownPosition = touch.position;
DetectSwipe();
}
if (touch.phase == TouchPhase.Ended)
{
fingerDownPosition = touch.position;
DetectSwipe();
}
}
}
private void DetectSwipe()
{
if (SwipeDistanceCheckMet())
{
if (IsVerticalSwipe())
{
var direction = fingerDownPosition.y - fingerUpPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
SendSwipe(direction);
}
else
{
var direction = fingerDownPosition.x - fingerUpPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
SendSwipe(direction);
}
fingerUpPosition = fingerDownPosition;
}
}
private bool IsVerticalSwipe()
{
return VerticalMovementDistance() > HorizontalMovementDistance();
}
private bool SwipeDistanceCheckMet()
{
return VerticalMovementDistance() > minDistanceForSwipe || HorizontalMovementDistance() > minDistanceForSwipe;
}
private float VerticalMovementDistance()
{
return Mathf.Abs(fingerDownPosition.y - fingerUpPosition.y);
}
private float HorizontalMovementDistance()
{
return Mathf.Abs(fingerDownPosition.x - fingerUpPosition.x);
}
private void SendSwipe(SwipeDirection direction)
{
SwipeData swipeData = new SwipeData()
{
Direction = direction,
StartPosition = fingerDownPosition,
EndPosition = fingerUpPosition
};
OnSwipe(swipeData);
}
}
public struct SwipeData
{
public Vector2 StartPosition;
public Vector2 EndPosition;
public SwipeDirection Direction;
}
public enum SwipeDirection
{
Up,
Down,
Left,
Right
}