- Home /
Load can only be called from the main thread
Load can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. UnityEngine.Resources:Load(String) ServerScript:DisplayText(String) (at Assets/ServerScript.cs:129) ServerScript:InitSocket() (at Assets/ServerScript.cs:43)
source is
public GameObject obj;
private Text txt;
// Use this for initialization
void Start ()
{
DisplayText("sdsf");
}
// Update is called once per frame
void Update () {
}
public void ServerOpenButton()
{
Thread t = new Thread(InitSocket);
t.IsBackground = true;
t.Start();
}
TcpListener server = null;
TcpClient clientSocket = null;
static int counter = 0;
public Dictionary<TcpClient, string> clientList = new Dictionary<TcpClient, string>();
private void InitSocket()
{
DisplayText(">> server start");
server = new TcpListener(IPAddress.Any, 9999);
clientSocket = default(TcpClient);
server.Start();
while (true)
{
try
{
counter++;
clientSocket = server.AcceptTcpClient();
DisplayText(">> client connect.");
NetworkStream stream = clientSocket.GetStream();
byte[] buffer = new byte[1024];
int bytes = stream.Read(buffer, 0, buffer.Length);
string user_name = Encoding.Unicode.GetString(buffer, 0, bytes);
user_name = user_name.Substring(0, user_name.IndexOf("$"));
clientList.Add(clientSocket, user_name);
// send message all user
SendMessageAll(user_name + " Joined ", "", false);
handleClient h_client = new handleClient();
h_client.OnReceived += new handleClient.MessageDisplayHandler(OnReceived);
h_client.OnDisconnected += new handleClient.DisconnectedHandler(h_client_OnDisconnected);
h_client.startClient(clientSocket, clientList);
}
catch (SocketException se)
{
Trace.WriteLine(string.Format("InitSocket - SocketException : {0}", se.Message));
break;
}
catch (Exception ex)
{
Trace.WriteLine(string.Format("InitSocket - Exception : {0}", ex.Message));
break;
}
}
clientSocket.Close();
server.Stop();
}
void h_client_OnDisconnected(TcpClient clientSocket)
{
if (clientList.ContainsKey(clientSocket))
clientList.Remove(clientSocket);
}
private void OnReceived(string message, string user_name)
{
string displayMessage = "From client : " + user_name + " : " + message;
DisplayText(displayMessage);
SendMessageAll(message, user_name, true);
}
public void SendMessageAll(string message, string user_name, bool flag)
{
foreach (var pair in clientList)
{
Trace.WriteLine(string.Format("tcpclient : {0} user_name : {1}", pair.Key, pair.Value));
TcpClient client = pair.Key as TcpClient;
NetworkStream stream = client.GetStream();
byte[] buffer = null;
if (flag)
{
buffer = Encoding.Unicode.GetBytes(user_name + " says : " + message);
}
else
{
buffer = Encoding.Unicode.GetBytes(message);
}
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}
}
public void DisplayText(string text)
{
GameObject tmp;
tmp = (GameObject) Instantiate(Resources.Load("Text"), obj.transform); //this line is 129
txt = tmp.GetComponent<Text>();
txt.text = text;
UnityEngine.Debug.Log(text);
}
Start -> sdsf print
but button click, and error
Unity api is not thread safe so when you try to use a Unity function (inside DisplayText) ,outside the main thread you end up with the error. Even if you remove the instatiate line the GetComponent will throw an error too as the debug.log will do.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
unity and MSSQL connection.. socket exception 0 Answers