Getting position of finger along a screen

| | August 5, 2015

I’m trying to get a list of Vector2 position by tracking my finger and then tracing them with a prefab (Basically just gestures). I have appended an onGUI function to help print out the list of vectors.

However, the attached code does not work and only prints “System.collection.Generic.List’1[UnityEngine.Vector2] when my finger moves along the screen.
I know I’m missing something; any help would be greatly appreciated.

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class line : MonoBehaviour
{

    List<Vector2> movePos = new List<Vector2>();
    private float lastFingerRelease;
    public String printer;

    void Start()
    {

    }


    void Update()
    {

        if (Input.touchCount > 0)
        {

            Touch touch = Input.GetTouch(0);

            if(touch.phase == TouchPhase.Began && Time.time - lastFingerRelease > 2)
            {


                printer = movePos.ToString();

                movePos.Clear();
            }

            else if (touch.phase == TouchPhase.Ended)
            {
                lastFingerRelease = Time.time;
            }

            movePos.Add(touch.position);
        }   

    }

   void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 1000, 1000), printer);
    }

}

One Response to “Getting position of finger along a screen”

  1. Gustavo Maciel on November 30, -0001 @ 12:00 AM

    Your question actually has nothing to do with its title, but here’s the answer anyway:

    ToString is a method of the Object class, which all classes inherit, but not all implement it. It seems List does not implement it, so the default behavior is to make a string with the just the name of the namespace and the class.

    That’s why you’re seeing a "System.collection.Generic.List'1[UnityEngine.Vector2]. It shows you the namespace, name and type of the class.

    If you really want to do something useful to print that list, try iterating over it and showing each of its items:

    int i = 0;
    foreach(Vector2 vec in movePos)
    {
        v += "[" + (i++) + "] = " + vec.ToString() + ";n";
    }
    printer = v;
    

    this should give you something in the lines of:

    [0] = (0.00, 0.00, 0.00);

    [1] = (0.11, 0.00, 0.00);

    [2] = (0.25, 0.10, 0.00);

    [3] = (0.30, 0.20, 0.10);

Leave a Reply