gpt4 book ai didi

c# - 从私有(private)void函数c#中的另一个类访问变量

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:43 25 4
gpt4 key购买 nike

我想从我的 myCSV 类中的 UDPReceive 类访问 myNumber 但它每次都返回 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,然后在后台线程启动时将其设置为 555在 UDPReceive 脚本的 Start() 方法中。

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

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

关于c# - 从私有(private)void函数c#中的另一个类访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38492564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com