Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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
Unity3d C#错误访问非静态成员需要对象引用(unity和arduino之间的串行通信)_Unity3d_Arduino - Fatal编程技术网

Unity3d C#错误访问非静态成员需要对象引用(unity和arduino之间的串行通信)

Unity3d C#错误访问非静态成员需要对象引用(unity和arduino之间的串行通信),unity3d,arduino,Unity3d,Arduino,出现错误“需要对象引用才能访问非静态成员。” 你知道如何解决这个问题吗? 我不想使用“static”,因为UnityGUI的“button-Onclick”不能使用静态成员 与此屏幕截图类似,如果我们在GUI上使用静态成员(画布按钮单击),则不会列出该静态成员(“缺少----------------”) 我的密码是blow 发送.cs using UnityEngine; using System.Collections; using System.IO.Ports; using System

出现错误“需要对象引用才能访问非静态成员。”

你知道如何解决这个问题吗? 我不想使用“static”,因为UnityGUI的“button-Onclick”不能使用静态成员

与此屏幕截图类似,如果我们在GUI上使用静态成员(画布按钮单击),则不会列出该静态成员(“缺少----------------”)

我的密码是blow

发送.cs

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class Sending : MonoBehaviour{
    //public static SerialPort sp = new SerialPort("COM4",9600,Parity.
    public SerialPort sp = new SerialPort("----------);
    public string message2;
    float timePassed = 0.0f;
    //Use this for initialization
    void Start()
    {
        OpenConnection();
    }

    //Update is called once per frame
    void Update(){
        //timePassed+= Time.deltaTime;
        //if(timePassed)=0.2f){
        //print("BytesToRead" +sp.BytesToRead);
        message2 = sp.ReadLine ();
        print (message2);
        ///timePassed = 0.0f;
        //}
    }

    public void OpenConnection()
    {
        if(sp != null)
        {
            if (sp.IsOpen) {
                sp.Close ();
                print ("Closing port, because it was already open!");
            }  else {
                sp.Open ();//open the connection
                sp.ReadTimeout = 16;
                print ("Port Opened!");
                //message = "Port Opened";
            }
        }
        else 
        {
            if (sp.IsOpen)
            {
                print ("Port is already open");
            }
            else
            {
                print ("Port == null");
            }
        }
    }
    void OnApplicationQuit()
    {
        sp.Close();
    }
    public void sendStop(){
        sp.Write ("s");
    }
    public void sendForward(){
        sp.Write("f");
    }
    public void sendBack(){
        sp.Write("b");
    }
    public void sendRight(){
        sp.Write("r");
    }
    public void sendLeft(){
        sp.Write("l");
    }
}
此外,还有一些脚本

CallForward.cs

using UnityEngine;
using System.Collections;
public class CallForward: MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnMouseDown(){
        print ("Clicked");
        Sending.sendForward();
    }
}

您必须先获得发送脚本,然后才能使用它。只有静态类可以在不首先启动它们的情况下使用

GameObject.Find("GameObjectWithSendingScript").GetComponent<Sending>().sendForward();
GameObject.Find(“GameObjectWithSendingScript”).GetComponent().sendForward();

本项目将unity与arduino连接,通过uGUI控制直流电机。像CallForward.cs一样,我有CallStop、Right、Left、Back.cs。我已经通过串行通信控制了unity对象“Cube”到arduino的直流电机。但是,它使用静态。通过使用相同的算法,我尝试编写uGUI。因此,uGUI>画布>按钮(Onclick缺失)。现在,我从Sending.cs public SerialPort sp=new SerialPort(“------------”);和public(static)void sendStop(){sp.Write(“s”);}中删除了static。public void sendForward、sendRight、sendlight、sendlight和sendBack都是相同的。@Kardux感谢您的回复。我已经查看了该页面,但我没有解决我的问题:(感谢您的建议。我应该附加哪个脚本“Sending.cs”或“CallForward.cs”?此代码用于在CallForward.cs中出现错误的地方。请尝试使用我的代码,而不是
Sending.sendForward()
@peter谢谢你,peter。谢谢你的宝贵建议。我解决了这个问题。你是上帝!一个伟大的工程师!!!!!哦,Greaattt!!!!!我真的很感激你!哈哈,很高兴能帮上忙!彼得是谁?哦,很抱歉误会了你的名字,我的上帝。你的名字是Fredrik,不是peter。我真的很感激你,因为你救了我很长时间调试艾美,两天!!!!!你是伟大的上帝。谢谢:)