编译错误cs8032

本文关键字:cs8032 错误 编译 | 更新日期: 2023-09-27 18:02:18

你好,我做了一个csharp脚本,它一直给我这个编译器错误(错误cs8032:解析过程中的内部编译器错误)。运行-v获取更多细节)。脚本如下:

using UnityEngine;  
using System.Collections;
using System.IO.Ports;
public class MoveObject : MonoBehaviour {

    public float speed;
    private float amoutToMove;
    SerialPort sp = new SerialPort("COM3",9600);
    void Start () {
        sp.Open();
        sp.ReadTimeout = 1;
    }
    void Update () {
        amoutToMove = speed * Time.deltaTime;
        if (sp.IsOpen) {
            try
            {
                MoveObject(sp.ReadByte());
                print(sp.ReadByte());
            }
            catch(System.Exception)
            {
            }
        }
    }
}
void MoveObject(int Direction)
{
    if (Direction == 2)
    {
        transform.Translate(Vector3.left * amoutToMove, Space.World);
    }
    if (Direction == 1)
    {
        transform.Translate(Vector3.right * amoutToMove, Space.World);
    }
}

编译错误cs8032

我猜您正在尝试创建具有相同名称的类和方法。尝试重命名方法。

还有一件事,你在你的方法中使用了amoutToMove变量,但它是在你的类中声明的,它不在你的方法的作用域中。这又是一个错误。但目前编译器不能给你这个错误,由于在类和方法相同的名称。

希望能有所帮助。

我想你应该这样重写

private SerialPort sp;
void Start () {
    sp = new SerialPort("COM3",9600);
    sp.Open();
    sp.ReadTimeout = 1;
}