I am using unity 3d to visualise a serial output from an Arduino and keep geting the error
FormatException: Input string was not in a correct format. System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Int32.Parse (System.String s, System.IFormatProvider provider) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Convert.ToInt32 (System.String value) (at <599589bf4ce248909b8a14cbe4a2034e>:0) One_mputest.Update () (at Assets/One_mputest.cs:48)
i have tried using plugins and they did not work
C# script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
using System.Linq;
public class One_mputest : MonoBehaviour {
private int x;
private int y;
private int z;
private int roll;
private int pitch;
private int yaw;
static SerialPort serial = new SerialPort("COM3", 9600);
// Use this for initialization
void Start () {
serial.ReadTimeout = 5000;
if (!serial.IsOpen) {
try {
serial.Open ();
} catch (TimeoutException) {
}
} else
Debug.LogError ("Port already open");
}
// Update is called once per frame
void Update () {
if (!serial.IsOpen)
serial.Open ();
string ypr = serial.ReadLine ();
List<String> stringList = ypr.Split('\t').ToList();
int[] intArray = new int[7];
for (int s = 0; s < 7; s++)
{
intArray[s] = Convert.ToInt32(stringList[s]);
}
x = intArray[3];
y = intArray[0];
z = intArray[1];
transform.localEulerAngles = new Vector3(z, y, x);
}
}
The serial input:
-104.65 -10 5 65536.00 0.00 0.00 0.00
-102.74 -4 1 65536.00 0.00 0.00 0.00
-102.19 -2 0 65536.00 0.00 0.00 0.00
-102.09 0 0 65536.00 0.00 0.00 0.00
-101.75 0 0 65536.00 0.00 0.00 0.00
-101.61 0 -1 65536.00 0.00 0.00 0.00
-101.62 0 -1 65536.00 0.00 0.00 0.00
-101.68 0 -1 65536.00 0.00 0.00 0.00
-101.76 0 -1 65536.00 0.00 0.00 0.00
-102.02 0 -1 65536.00 0.00 0.00 0.00
-102.20 0 -1 65536.00 0.00 0.00 0.00
-102.33 0 -1 65536.00 0.00 0.00 0.00
-102.43 0 -1 65536.00 0.00 0.00 0.00
-102.57 0 -1 65536.00 0.00 0.00 0.00
-102.77 0 -1 65536.00 0.00 0.00 0.00
-102.90 0 -1 65536.00 0.00 0.00 0.00
-102.68 0 -1 65536.00 0.00 0.00 0.00
-102.48 0 -1 65536.00 0.00 0.00 0.00
-102.26 0 -1 65536.00 0.00 0.00 0.00
-102.01 0 -1 65536.00 0.00 0.00 0.00
-101.83 0 -1 65536.00 0.00 0.00 0.00
-102.15 0 -1 65536.00 0.00 0.00 0.00
-102.34 0 -1 65536.00 0.00 0.00 0.00
-102.17 0 -1 65536.00 0.00 0.00 0.00
-101.89 0 -1 65536.00 0.00 0.00 0.00
-101.74 0 -1 65536.00 0.00 0.00 0.00
-101.88 0 -1 65536.00 0.00 0.00 0.00
-101.95 0 -1 65536.00 0.00 0.00 0.00
-102.01 0 -1 65536.00 0.00 0.00 0.00
-102.05 0 -1 65536.00 0.00 0.00 0.00
-102.06 0 -1 65536.00 0.00 0.00 0.00
-102.00 0 -1 65536.00 0.00 0.00 0.00
-101.96 0 -1 65536.00 0.00 0.00 0.00
-102.06 0 -1 65536.00 0.00 0.00 0.00
-102.14 0 -1 65536.00 0.00 0.00 0.00
-102.37 0 -1 65536.00 0.00 0.00 0.00
-102.81 0 -1 65536.00 0.00 0.00 0.00
I expected the vector variable to change and move the object that the script was applied to
and only got the error about: Input string was not in a correct format
stringList[s]andstringList[s].Lengthwhen the exception is thrown?