我想從UDPReceive
類訪問myNumber
類在我的myCSV
類中,但它每次返回0。訪問一個私人空白函數c中的另一個類的變量#
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;
它也沒有工作。我真的不知道我做錯了什麼。
您確定ReceiveData()已被調用,並且其中的所有條件都需要設置myNumber嗎? – John3136