- Home /
Unity Threading Issue: Not able to delegate task
I am using a raspberry pi to send information to Unity via UDP. In order to circumvent the multi-threading issue with Unity, I am delegating a task in a part of the script. I am getting an error stating:
"error CS0120: An object reference is required to access non-static member `UDPReceive.te'"
How can I properly send the information using this task delegation technique? I have the raspberry pi sending information properly to Unity but am unable to retrieve the information due to the threading issue, which is what I am trying to work around here.
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPReceive : MonoBehaviour {
// receiving Thread
Thread receiveThread;
// udpclient object
UdpClient client;
// public
public string IP = "128.235.117.210";
public int port; // define > init
public TaskExecutorScript te;
// NEW
public int X;
public int Y;
string[] split_data;
public static GameObject cube;
// infos
public string lastReceivedUDPPacket="";
private string info;
// start from shell
private static void Main()
//public void Update()
{
UDPReceive receiveObj=new UDPReceive();
receiveObj.init();
string text="";
do
{
text = Console.ReadLine();
}
while(!text.Equals("exit"));
// NEW
te.ScheduleTask(new Task(delegate
{
// Add whatever Unity API code you want here
//split_data = text.Split(' ');
//X = Int32.Parse(split_data [0]);
//Y = Int32.Parse(split_data [1]);
transform.position = new Vector3(0, 0, 0);
}
));
}
// start from unity3d
public void Start()
{
te = GetComponent<TaskExecutorScript> ();
init();
// cube = GameObject.Find ("Cube");
}
// OnGUI
void OnGUI()
{
Rect rectObj=new Rect(40,10,200,400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj,"# UDPReceive\n"+IP+": "+port+" #\n"
+ "shell> nc -u "+IP+": "+port+" \n"
+ "\nLast Packet: \n"+ lastReceivedUDPPacket
,style);
}
// init
private void init()
{
// Endpunkt definieren, von dem die Nachrichten gesendet werden.
print("UDPSend.init()");
// define port
port = 6666;
// status
print("Sending to "+IP+": "+port);
print("Test-Sending to this Port: nc -u "+IP+": "+port+"");
// ----------------------------
// Abhören
// ----------------------------
// Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
// Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// receive thread
private void ReceiveData()
{
client = new UdpClient(port);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, port);
byte[] data = client.Receive(ref anyIP);
// Bytes mit der UTF8-Kodierung in das Textformat kodieren.
string text = Encoding.UTF8.GetString(data);
// Den abgerufenen Text anzeigen.
print(">> " + text);
// latest UDPpacket
lastReceivedUDPPacket=text;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
// getLatestUDPPacket
// cleans up the rest
public string getLatestUDPPacket()
{
return lastReceivedUDPPacket;
}
/*
public void UseInfo()
{
info = lastReceivedUDPPacket;
arraySplit = info.Split ('0');
x = int.Parse(arraySplit [0]);
y = int.Parse(arraySplit [1]);
this.transform.position = new Vector3 (x, y, 0);
} */
void OnDisable()
{
if ( receiveThread!= null)
receiveThread.Abort();
client.Close();
}
}
and
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public delegate void Task();
public class TaskExecutorScript : MonoBehaviour {
private Queue<Task> TaskQueue = new Queue<Task>();
private object _queueLock = new object();
// Update is called once per frame
void Update () {
lock (_queueLock)
{
if (TaskQueue.Count > 0)
TaskQueue.Dequeue()();
}
}
public void ScheduleTask(Task newTask)
{
lock (_queueLock)
{
if (TaskQueue.Count < 100)
TaskQueue.Enqueue(newTask);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to use multi-threading on Hololens 1 Answer
Update Method doesn't see value set by other thread 0 Answers
Google Daydream Thread with Unity 1 Answer