I receive the following error
"INTERNAL_CALL_Internal_InstantiateSingle 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."
whenever I try to instantiate some GameObject's
using this function:
public void DisplayCalls () {
for (int i = 0; i < calls [0].Count; i++) {
for (int j = 0; j < impFields.Length; j++) {
GameObject textClone = Instantiate (textBox, Vector3.zero, Quaternion.Euler(Vector3.zero)) as GameObject;
textClone.transform.SetParent (canvas.transform);
Text text = textClone.GetComponent <Text> ();
text.text = calls[impFields [j]][i];
text.alignment = TextAnchor.MiddleCenter;
textClone.transform.localPosition = new Vector3 (-262.5f + (175f * j), (182f + (30f * i)), 0f);
}
}
}
The function is called using this other function:
public void GetPanelInfo () {
string file = "getpanelinfo.php";
string hash = Md5Sum (mostRecentID.ToString () + SecretKey1 + SecretKey2);
string parameters = "?mostrecentid=" + mostRecentID.ToString () + "&hash=" + hash;
string fullURL = baseURL + file + parameters;
Uri fullURI = new Uri (fullURL);
WebClient client = new WebClient ();
string jsonString = string.Empty;
client.DownloadStringCompleted += (sender, e) => {
if (!e.Cancelled && e.Error == null) {
jsonString = e.Result;
JSONNode json = JSON.Parse (jsonString);
for (int i = 0; i < json["calls"].Count; i++) {
for (int j = 0; j < calls.Length; j++) {
calls[j].Add (json["calls"][i][names [j]]);
}
mostRecentID = json["calls"][i]["callID"].AsInt;
}
} else if (e.Cancelled && e.Error == null) {
Debug.Log ("Cancelled");
} else {
Debug.Log ("Error: " + e.Error);
}
loading = false;
LogCalls ();
DisplayCalls ();
};
client.DownloadStringAsync (fullURI);
}
Which is called once using a button.
I cannot seem to find the problem, but as stated above, the error says that it is a threading problem and that I cannot call instantiate from another thread other than the main one. I do not know how to change the instantiation to another thread, but any help to solve this problem would be much appreciated.