机械臂数字挛生问题询问,请教使用unity进行跨脚本读取6个关节变数参数问题。

目前有参考板上博主的跨脚本方式:
第一种,被调用脚本函数为static类型,调用时直接用 脚本名.函数名()。

第二种,GameObject.Find("脚本所在物体名").SendMessage("函数名"); 此种方法可以调用public和private类型函数

第三种,GameObject.Find("脚本所在物体名").GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数
原文链接:https://blog.csdn.net/smilingeyes/article/details/17767269

但是目前遇到问题,目前想要读取List r_robottarget = robottarget.Split(',').ToList();这一段资讯,这里面是我从伺服端丢过来的6个关节变数。

unity 想要跨脚本的程式:

    /// <summary>
    /// 信息接收方法的回調
    /// </summary>
    /// <param name="ar"></param>
    public void ReceiveFormServerCallBack(IAsyncResult ar)
    {
        //結束接收
        int length = clientSocket.EndReceive(ar);
        byte[] buffer = (byte[])ar.AsyncState;
        //將接收的東西轉為字符串
        string msg = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
        //Debug.Log(msg); //接收到的消息是 //"接收到的消息是"+

        //開啟下一次接收消息
        ReceiveFormServer();

        string robottarget = Convert.ToString(msg);
        robottarget = string.Join("", robottarget.Split());

        List<string> r_robottarget = robottarget.Split(',').ToList();
        Debug.Log(robottarget);
        Debug.Log(r_robottarget);

    }

跨脚本程式测试:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System;
using System.Collections.Generic;
using System.Linq;

public class joint1 : MonoBehaviour
{
    /// <param name="ar"></param>
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //GameObject.Find("tounity").GetComponent<ClientSocketController>().ReceiveFormServerCallBack();
    }
}

跳出的error:

img

如果将IAsyncResult ar加进去的话,error会跳出:

img

请教一下各位要怎么解决,感谢!

你这方法大概是接受服务端数据的异步回调函数,怎么会在Update里去调用呢?你这客户端和服务端的通信逻辑有问题哦

第三种,GameObject.Find("脚本所在物体名").GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数?不是的哦,也可以访问你这脚本里面的属性和变量等,定义为public即可,

或者你将该脚本设置为单例,最简单的单例脚本,定义:public static 脚本名 instance;
在start、或awake方法里写instance=this;
需要访问的变量或属性定义为public,或者访问器的形式,比如:public float a;
那你在脚本访问时这样写:float b= 脚本.instance.a 即可

你的list要定义为全局的public
public List r_robottarget;