Search Unity

Load can only be called from Main Thread. [HELP]

Discussion in 'Scripting' started by samasaurus6, Apr 2, 2015.

  1. samasaurus6

    samasaurus6

    Joined:
    Dec 18, 2014
    Posts:
    1
    Hello, I'm fairly new to Unity and am trying to make a game involving making words. To see if the text the user enters is a word, I'm using this dictionary lookup script:

    Code (JavaScript):
    1. import System.Collections.Generic;
    2. import System.Linq;
    3. var myDictionary : Dictionary.<String, String>;
    4. var wordToCheck : String;
    5. var MytextAsset = Resources.Load("dictionary", typeof(TextAsset))  as TextAsset;
    6.  
    7. function Start()
    8. {
    9. myDictionary = MytextAsset.text.Split("\n"[0]).ToDictionary(function(w){return w;});
    10.  
    11.     if(checkWord(wordToCheck))
    12.         Debug.Log(wordToCheck + " is a valid word");
    13.     else if(!checkWord(wordToCheck))
    14.         Debug.Log(wordToCheck + " is NOT a valid word");
    15. }
    16. function checkWord(word : String)
    17. {
    18.     return myDictionary.ContainsKey(word);    
    19. }
    20.  
    21. function Update()
    22. {
    23.  
    24. }
    The error I am getting is:
    ArgumentException: Load 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.
    DictionaryLookup..ctor () (at Assets/DictionaryLookup.js:6)

    I realise that I shouldn't load the TextAsset when first declaring it, but I am unsure how I would go about declaring it and then actually loading it from the Start() method.

    Any help would be much appreciated. Although I do get this error, the lookup actually functions and returns correct results based on the entered word. This script is just attached to the camera object in an empty scene.

    Sorry if this is something terribly simple.

    ~Sam.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Just move it on down to the Start().

    Code (csharp):
    1.  
    2. var MytextAsset;
    3.  
    4. function Start()
    5. {
    6.      MytextAsset = Resources.Load("dictionary", typeof(TextAsset))  as TextAsset;
    7.      // your other code
    8. }
    9.