2016-07-20 54 views
1

我想從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; 

它也沒有工作。我真的不知道我做錯了什麼。

+3

您確定ReceiveData()已被調用,並且其中的所有條件都需要設置myNumber嗎? – John3136

回答

4

您的myCSV腳本可能正在啓動UDPReceive腳本之前。

您正在訪問的myCSV腳本UDPReceive.myNumberStart()方法之前,它在後臺線程設置爲555開始在UDPReceive腳本的Start()方法。

您應該正在訪問UDPReceive腳本的Update()方法的靜態myNumber變量。該Update()方法會被連續調用所有腳本(包括您myCSV腳本)的初始化之後,因此變量myVariable將被設置爲555

且不說變量myVariable應被聲明爲volatile

+0

不錯的傢伙謝謝 – 2222

相關問題