0
\$\begingroup\$

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);
    }

}
\$\endgroup\$
  • \$\begingroup\$ It depends how you want to store them... If you want to track the exact path you could add the finger position at intervals or you could just save the start position and end position and then animate it. What are you intending on doing? \$\endgroup\$ – Savlon Jun 3 '15 at 10:02
1
\$\begingroup\$

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);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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