I am using Unity 5.0.
I am trying to download some file using C# web client asynchronous.
File gets downloaded and DownloadDataCompleted
events also gets fired.
Then I try to do some calculation but I get these error.
RandomRangeInt
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.
Any solution to above problem or can anyone tell me how to run that calculation part on main thread
As requested code is here
private void DownloadXMLFromServer()
{
//first download the file from server
WebClient _maClient = new WebClient ();
_maClient.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e)
{
File.WriteAllBytes("DownloadedXML.xml",e.Result);
DownloadImgOfApp();
};
_maClient.DownloadDataAsync (new Uri (instance.urlOf_XML));
}
public void DownloadImgOfApp()
{
int appNumber = UnityEngine.Random.Range (0, totalNumbOfAppsAvaialbeInXML);//these line throws error
string appName = "App" + (appNumber + 1);
string downloadImgLinkName = null;
string clickableLink = null; }