-1

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;   }
Mykola
  • 3,343
  • 6
  • 23
  • 39
user2810313
  • 79
  • 2
  • 9
  • 1
    What is the problem? This is not about "calculations", it is a specific method that can not be called. Which totally looks like you can work around it, or? RandomRangeInt hardly sounds like a method that can not be replaced. – TomTom Jan 11 '16 at 12:33
  • Could you show us some of your code? Would help a lot. – Christoph K Jan 11 '16 at 12:33
  • And it is also a change in Unity 5. I guess (only guess) that you are using some `Singleton` class in which methods are being called from initializer or some sort of that. If you show some code then that would be more appreciated – Hamza Hasan Jan 11 '16 at 12:43
  • Separate variable declaration from variable initialize (or define from init). Let's say - [1st step]- declare it public int myRandom; Then somewhere in your void Start () - [2nd step] - give a value to myRandom as myRandom = Random.Range(1, 5); – ares777 Jan 11 '16 at 12:45
  • Ok even if I use PlayerPrefs.GetFloat("String") or StartCoroutine it gives same error .....can only be called from the main thread. Constructors and field initializers will be executed from........ – user2810313 Jan 12 '16 at 04:31

1 Answers1

1

A lot of methods in Unity3D can be called just from the main thread. This means they can only be called in Start() Update() Awake() OnTriggerEnter(...) and so on. Unity will not let you call these methods from different threads (including asynchronous callbacks).

This makes sense for things like GameObject.Instatiate(...), but I cannot think of a reason why random could cause problems. But for some reason Unity is blocking calls anyway.

To solve your problem, you can create an instance of System.Random and generate your random numbers with it.
The only Problem I can see is that System.Random cant give you a float, just bytes, int or double.

AntiHeadshot
  • 1,130
  • 9
  • 24
  • Random.Range is also in UnityEngine namespace. so Unity will not allow also Random.Range. But you can use System.Range – Cenkisabi Jan 11 '16 at 14:29