从私有无效函数 c# 中的另一个 class 访问变量

Accessing a variable from another class inside a private void function c#

我想从我的 myCSV class 中的 UDPReceive class 访问 myNumber 但每次都是 returns 0。

using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class UDPReceive : MonoBehaviour
{
    Thread receiveThread;
    UdpClient client;
    public int port;
    public int sizeOfData = 0;
    public string time = "";
    bool myDataGram = false;

    public static float myNumber;

    //********************************************************
    //                       MAIN
    //********************************************************
    private static void Main()
    {
        UDPReceive receiveObj = new UDPReceive();
        receiveObj.init();
        string text = "";

        do
        {
            text = Console.ReadLine();
        }
        while (!text.Equals("exit"));
    }

    //********************************************************
    //                      START
    //********************************************************

    public void Start()
    {
        init();
    }

    //********************************************************
    //                       ONGUI
    //********************************************************
    void OnGUI()
    {
        GUIStyle style = new GUIStyle();
        style.alignment = TextAnchor.UpperLeft;
        GUI.Box((new Rect(40, 10, 600, 600)), "Time: " + myNumber, style);  
    }

    //********************************************************
    //                       INIT
    //********************************************************
    private void init()
    {
        print("UDPSend.init()");
        port = 3500;

        receiveThread = new Thread(new ThreadStart(ReceiveData));
        receiveThread.IsBackground = true;
        receiveThread.Start();

    }

    //********************************************************
    //                       RECEIVEDATA
    //********************************************************
    private void ReceiveData()
    {
        client = new UdpClient(port);

        while (true)
        {
            try
            { 
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = client.Receive(ref anyIP); //THIS IS YOUR DATA

                myDataGram = true;

                if (myDataGram == true)
                {


                float myNumber2 = 555;
                myNumber = myNumber2;

                }


                //Thread.Sleep(1000);




            }
            catch (Exception err)
            {
                print(err.ToString());
            }     
        }
    }
}

我想访问它的脚本是这样的:

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

public class myCSV : MonoBehaviour
{

    public TextAsset file;
    public float mynewX;
    public float mynewY;
    public float mynewZ;
    private static float myNumbersss2;

    void Start()
    {


        Load(file);

             for (int i = 1; i < 6481; i++)
         {

         string j = i.ToString();
         float mynewX = Single.Parse(Find_point_number(j).my_x);
         float mynewY = Single.Parse(Find_point_number(j).my_y);
         float mynewZ = Single.Parse(Find_point_number(j).my_z);


             //print(mynewX);
             //print(mynewY);
             //print(mynewZ);
             //print(Find_point_number(j).my_x);
             //print(Find_point_number(j).my_y);                                                   
             //print(Find_point_number(j).my_z);


             GameObject prefab = Resources.Load("Cube") as GameObject;
             GameObject go = Instantiate(prefab);
             go.transform.position = new Vector3(mynewX, mynewY, mynewZ);



             //float myNumbersss2 = UDPReceive.myNumbersss3;




            float myNumbersss2 = UDPReceive.myNumber;
            print("from myCSV" + myNumbersss2);

        }



    }


}

我已经尝试了很多东西,但它们都不起作用。我一直得到 0。我正在统一使用它并且我已经尝试过

GameObject.Find("MainCamera").GetComponent<UDPReceive>().myNumber;

也没有用。我真的不知道我做错了什么。

您的 myCSV 脚本可能在 UDPReceive 脚本之前启动。

您正在访问 myCSV 脚本中 Start() 方法中的 UDPReceive.myNumber,然后它在 Start() 方法中启动的后台线程中设置为 555 =11=]脚本。

您可能应该从 UDPReceive 脚本的 Update() 方法访问静态 myNumber 变量。 Update() 方法将在所有脚本(包括您的 myCSV 脚本)初始化后不断调用,因此变量 myVariable 将设置为 555。

更不用说变量myVariable应该声明为volatile