Find can only be called from the main thread
And an error will appear on the console
If you'd like, I'd like you to give me a solution why I get an error.
Debug was done. The boss in the solution
If i reference the text object in Start and change the value in Update
I said I could do it, but I didn't understand it well.
Can you tell me if you like?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine.UI;//<---- Add 1
public class UdpSocket: MonoBehaviour
{
private string _MulticastAddr ="224.0.23.0" ;;// multicast address
private string _RemoteHost ="" ;;// Sender address
private int _SendPort = 3610;// send port
private int _RecvPort = 3610;// Receive port
private UdpClient _UdpClient;// UDP
private IPEndPoint _IpEndPoint;// IPEndPoint
private Thread _RecvThread;// receive thread
// Connect
public void Connect ()
{
IPAddress grpAddr = IPAddress.Parse (_MulticastAddr);
if (_IpEndPoint! = null) _IpEndPoint = null;
_IpEndPoint = new IPEndPoint (grpAddr, _RecvPort);
// Join a multicast group
Disconnect ();
_UdpClient = new UdpClient (_RecvPort);
_UdpClient.JoinMulticastGroup (grpAddr);
// Create receive thread
_RecvThread = new Thread (ReceiveMulticastThread);
_RecvThread.Start ();
// Send node profile notification
SendNodeProfile ();
}
// Disconnect
public void Disconnect ()
{
if (_RecvThread! = null)
{
_RecvThread.Abort ();
_RecvThread = null;
}
if (_UdpClient! = null)
{
IPAddress grpAddr = IPAddress.Parse (_MulticastAddr);
_UdpClient.DropMulticastGroup (grpAddr);
_UdpClient.Close ();
_UdpClient = null;
}
}
// Send node profile notification
public void SendNodeProfile ()
{
byte [] pack = BuildNodeProfileInfo ();
SendPacket (pack, _MulticastAddr);
}
// Send
public void SendPacket (byte [] packet, string host)
{
_UdpClient.Send (packet, packet.Length, host, _SendPort);
}
// Receiving thread
public void ReceiveMulticastThread ()
{
byte [] packet;
int i = 0;
System.Text.StringBuilder s = new System.Text.StringBuilder ();
while (true)
{
packet = _UdpClient.Receive (ref _IpEndPoint);
if (packet! = null)
{
// Received!
s.Remove (0, s.Length);
for (i = 0;i<packet.Length;i ++)
{
s.Append (System.Convert.ToString (packet [i], 16) .PadLeft (2,'0'));
}
Debug.Log (s.ToString ());
Text targetText;//<---- Add 2
GameObject _Object;
_Object = GameObject.Find ("Text");
targetText = _Object.GetComponent<Text>();//<---- Add 3
targetText.text ="s.ToString ()" ;;//<---- add 4
}
}
}
// Create node profile notification packet
private byte [] BuildNodeProfileInfo ()
{
byte [] pack;
pack = new byte [17];
pack [0] = 0x10;// EHD1
pack [1] = 0x81;// EHD2
pack [2] = 0x00;//
pack [3] = 0x01;// ID
pack [4] = 0x0E;// Source "Node Profile Class"
pack [5] = 0xF0;// EOJ = 0x0E F0 01
pack [6] = 0x01;//
pack [7] = 0x0E;// Destination "node profile class"
pack [8] = 0xF0;// EOJ = 0x0E F0 01
pack [9] = 0x01;//
pack [10] = 0x73;// ESV
pack [11] = 0x01;// OPC
pack [12] = 0xD5;// EPC
pack [13] = 0x04;// PDC
pack [14] = 0x01;// EDT
pack [15] = 0x05;//
pack [16] = 0xFF;//
pack [16] = 0x01;//
return pack;
}
}
Code
`` `
-
Answer # 1
-
Answer # 2
targetText.text ="s.ToString ()" ;;//<---- add 4
The GUI component display can only be changed from the main thread
If you go around with "c # separate thread control", you can find out how to deal with that.
Related articles
- c # - i want to get the header of the http response
- i want to pass the structure secured in c # to c ++
- i want to get the working directory in c #
- c # - i want to know the intent of mvvm "viewmodel must not know view"
- i want to implement the c # as operator in c ++
- i want to print the specified book of xlsx file in c #
- c # - i get an error in the if statement
- c # - i want to get the bool in scriptableobject
- c # - i want to change the font color for each cell with npoi
- [c #] i want to combine the acquired data
- c # - i want to update the value
- c # - i want to use addvectorobs ()
- javascript - i want to save the data of the duplicated form
- css - i want to specify only the part in svg
- html - i want to align the beginning of the line
- javascript - i want to pass the url with the get method
- java - i want to format a variable in the text
- html - i want to change the font size of v-btn
- dns - i want to fix the global ip
- php - i want to receive the db id with $_post [] but i can't
- c # - player production in unity (shooting game)
- c # - please tell me how to use the additional components of unity's volunteer production
- c # - conversion from enum type to enum type
- c # - missingreferenceexception occurs even though there is a reference
- c # - i want to update the value
- c # - i want the camera to follow the player character in a tps game, but if i make multiple players in multiplayer, the camera
- c # - [unity] even though "has exit time" of animator is specified, it will transition to another state earlier than t
- c # - == becomes false even though the strings are equal
- c # - how to use start () in unity
- c # - in a script? error cs1056: unexpected character'?' is displayed even though there is no
unity cannot use the unity function from another thread.
So instead of setting the text directly in the receiving thread
Try to do it differently.
I'm sorry, I don't know about unity so specific
I can't show you how.
The part that sets Text to targetText is not good enough.
Make a distinction between constants and variables.
・ Appendix
About your boss's plan
1. Create a private string type variable in the UdpSocket class you are creating.
2. In the receiving thread, instead of setting the received data directly in Text, just store it in the previous variable.
3. Create a start method in the UdpSocket class and get a Text instance there.
4). Create an update method in the UdpSocket class, and set the variable of 1 to the Text instance stored in 3.
If you write simply, you will see a flow like this.
The code is written appropriately, so please use it only as a reference for thinking.