How to call SendMessage from my thread , When i call it ,always alert error that “can only be called from the main thread.” not only SendMessage,even (if gameObject == null) and so on…
Try this:
using UnityEngine;
using System.Collections;
using System.Threading;
public class first : MonoBehaviour {
Thread firstThread;
public static bool checked_ = false;
public static string message;
public static string messageParam;
void Start()
{
firstThread = new Thread(ThreadFunc);
firstThread.Start();
}
void Update()
{
if (checked_)
{
checked_ = false;
SendMessage(message, messageParam);
}
}
void Reciewer(string text)
{
Debug.Log(text);
}
static void ThreadFunc()
{
message = "Reciewer";
messageParam = "Hello from thread";
checked_ = true;
}
}