title: Use Unity API from another Thread or call a function in the main Thread
body: <p>My problem is I try to use Unity socket to implement something. Each time, when I get a new message I need to update it to the updattext (it is a Unity Text). However, When I do the following code, the void update does not calling every time. </p>
  
  <p>The reason for I do not include <code>updatetext.GetComponent&lt;Text&gt;().text = "From server: "+tempMesg;</code>in the void getInformation is this function is in the thread, when I include that in getInformation() it will come with an error: </p>
  
  <blockquote>
    <p><code>getcomponentfastpath can only be called from the main thread</code></p>
  </blockquote>
  
  <p>I think the problem is I don't know how to run the main thread and the child thread in C# together? Or there maybe other problems.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System;
  using System.Net.Sockets;
  using System.Text;
  using System.Threading;
  using UnityEngine.UI;
  
  
  public class Client : MonoBehaviour {
  
      System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
      private Thread oThread;
  
  //  for UI update
      public GameObject updatetext;
      String tempMesg = "Waiting...";
  
      // Use this for initialization
      void Start () {
          updatetext.GetComponent&lt;Text&gt;().text = "Waiting...";
          clientSocket.Connect("10.132.198.29", 8888);
          oThread = new Thread (new ThreadStart (getInformation));
          oThread.Start ();
          Debug.Log ("Running the client");
      }
  
      // Update is called once per frame
      void Update () {
          updatetext.GetComponent&lt;Text&gt;().text = "From server: "+tempMesg;
          Debug.Log (tempMesg);
      }
  
      void getInformation(){
          while (true) {
              try {
                  NetworkStream networkStream = clientSocket.GetStream ();
                  byte[] bytesFrom = new byte[10025];
                  networkStream.Read (bytesFrom, 0, (int)bytesFrom.Length);
                  string dataFromClient = System.Text.Encoding.ASCII.GetString (bytesFrom);
                  dataFromClient = dataFromClient.Substring (0, dataFromClient.IndexOf ("$"));
                  Debug.Log (" &gt;&gt; Data from Server - " + dataFromClient);
  
                  tempMesg = dataFromClient;
  
                  string serverResponse = "Last Message from Server" + dataFromClient;
  
                  Byte[] sendBytes = Encoding.ASCII.GetBytes (serverResponse);
                  networkStream.Write (sendBytes, 0, sendBytes.Length);
                  networkStream.Flush ();
                  Debug.Log (" &gt;&gt; " + serverResponse);
  
              } catch (Exception ex) {
                  Debug.Log ("Exception error:" + ex.ToString ());
                  oThread.Abort ();
                  oThread.Join ();
              }
  //          Thread.Sleep (500);
          }
      }
  }
  </code></pre>