Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何修复输入字符串格式不正确的unity 3d和serial_C#_Unity3d_Serial Port - Fatal编程技术网

C# 如何修复输入字符串格式不正确的unity 3d和serial

C# 如何修复输入字符串格式不正确的unity 3d和serial,c#,unity3d,serial-port,C#,Unity3d,Serial Port,我使用unity 3d可视化Arduino的串行输出,并不断获取错误 FormatException:输入字符串的格式不正确。 System.Number.StringToNumber(System.String str, System.Globalization.numberstyle选项,System.Number+NumberBuffer& 数字,System.Globalization.NumberFormatInfo,System.Boolean (十进制)(at:0) System.

我使用unity 3d可视化Arduino的串行输出,并不断获取错误

FormatException:输入字符串的格式不正确。 System.Number.StringToNumber(System.String str, System.Globalization.numberstyle选项,System.Number+NumberBuffer& 数字,System.Globalization.NumberFormatInfo,System.Boolean (十进制)(at:0) System.Number.ParseInt32(System.String s, System.Globalization.NumberStyles风格, System.Globalization.NumberFormatInfo)(位于 :0)System.Int32.Parse (System.String s,System.IFormatProvider提供程序)(位于 :0)System.Convert.ToInt32 (System.String值)(位于:0) One_computerst.Update()(位于Assets/One_computerst.cs:48)

我试过使用插件,但都不起作用

C#脚本:

我希望vector变量会更改并移动脚本应用到的对象


并且只得到以下错误:输入字符串的格式不正确

Try&catch是您的朋友。文件中可能有一个额外的
\t
,您正试图将
null
转换为某个数字。还有一个很好的技巧就是总是在if语句周围使用{},以确保没有意外的事情发生(例如:您认为if语句内部有东西,但它在外部)

我想了解的一点是,您正在尝试将表示
浮点值的
字符串
转换为
Int32
,这可能是出现问题的地方

试着这样做:

void Update () {
    var whatdoing = "starting try";

    try {
        if (!serial.IsOpen) {
            serial.Open (); }

        whatdoing               = "reading line";
        string ypr              = serial.ReadLine ();

        List<String> stringList = ypr.Split('\t').ToList();
        int[] intArray          = new int[7];

         for (int s = 0; s < 7; s++) {
             whatdoing = "converting: " + stringList[s];

             if(!Int.TryParse(stringList[s], out intArray[s]) {
                 throw new Exception("Could not parse string to Int: "+whatdoing); } }
          whatdoing = "Assigning xyz";
          x         = intArray[3];
          y         = intArray[0];
          z         = intArray[1];

          transform.localEulerAngles = new Vector3(z, y, x);}
catch(Exception e) {
 BREAK HERE and check exception & whatdoing.
} }
void更新(){
var whatdoing=“开始尝试”;
试一试{
如果(!serial.IsOpen){
serial.Open();}
whatdoing=“阅读线”;
字符串ypr=serial.ReadLine();
List stringList=ypr.Split('\t').ToList();
int[]intArray=新的int[7];
对于(int s=0;s<7;s++){
whatdoing=“转换:”+stringList[s];
如果(!Int.TryParse(stringList[s],out intArray[s]){
抛出新异常(“无法将字符串解析为Int:+whatdoing);}
whatdoing=“分配xyz”;
x=阵列[3];
y=intArray[0];
z=阵列[1];
transform.localEulerAngles=新向量3(z,y,x);}
捕获(例外e){
在这里中断并检查异常和正在进行的操作。
} }

将字符串解析为正确的值是出了名的棘手。一般来说,您应该使用TryParse()。但是,由于您没有告诉我们您希望将每个串行输入解析为哪个值,我们无法帮助您。我的意思是“-104.65-10 5 65536.00 0.00 0.00 0.00 0.00 0.00 0.00”应该解析为什么数字?stringList[s]的值是什么
stringList[s]。引发异常时的长度
-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   
void Update () {
    var whatdoing = "starting try";

    try {
        if (!serial.IsOpen) {
            serial.Open (); }

        whatdoing               = "reading line";
        string ypr              = serial.ReadLine ();

        List<String> stringList = ypr.Split('\t').ToList();
        int[] intArray          = new int[7];

         for (int s = 0; s < 7; s++) {
             whatdoing = "converting: " + stringList[s];

             if(!Int.TryParse(stringList[s], out intArray[s]) {
                 throw new Exception("Could not parse string to Int: "+whatdoing); } }
          whatdoing = "Assigning xyz";
          x         = intArray[3];
          y         = intArray[0];
          z         = intArray[1];

          transform.localEulerAngles = new Vector3(z, y, x);}
catch(Exception e) {
 BREAK HERE and check exception & whatdoing.
} }