adding dictionary

i am making a word game in which when letters are pressed a word is formed and then will be compare with the Dictionary if the word user provided is valid or not. for that purpose we used a code provided on unity forum by "Jon Petersen " to add Dictionary

#pragma strict

import System.Collections.Generic;
import System.Linq;
var sphe: GameObject;
var myDictionary : Dictionary.<String, String>;
var wordToCheck : String;
function OnMouseDown()
{
if(gameObject.name=="Sphere"){
Update();
}
}
function Awake() 
{
    var MytextAsset = Resources.Load("dictionary", typeof(TextAsset))  as TextAsset;
    myDictionary = MytextAsset.text.Split("

"[0]).ToDictionary(function(w){return w;});
}

function checkWord(word : String) 
{
    return myDictionary.ContainsKey(word);   
}
 
function Update()
{
    if(checkWord(chk1.word))
       Debug.Log(chk1.word + " is a valid word");
    else if(!checkWord(chk1.word))
       Debug.Log(chk1.word + " is NOT a valid word");
}

we added onMouseDown function so when a game object is pressed it start comparing. we are providing the word to compare when the game is running it is giving an error ’

" NullReferenceException: Object reference not set to an instance of an object
dictionary_code.Awake () (at Assets/scripts/dictionary_code.js:17) "
this error on this function

function Awake() 
    {
        var MytextAsset = Resources.Load("dictionary", typeof(TextAsset))  as TextAsset;
        myDictionary = MytextAsset.text.Split("

"[0]).ToDictionary(function(w){return w;});

" NullReferenceException: Object reference not set to an instance of an object
dictionary_code.checkWord (System.String word) (at Assets/scripts/dictionary_code.js:22)
dictionary_code.Update () (at Assets/scripts/dictionary_code.js:27 "
this error on this line

return myDictionary.ContainsKey(word);

don’t know what to do please help.

The first error says that the Object "MyTextAsset" does not have a value. You try to load the asset on line 16, which means that the load function fails to find the asset in your project. Check the spelling of the word in the load function ("dictionary") to see if it matches your assets name exactly (case sensitive). Also make sure your text asset is in a folder called "Resources", only assets in that folder can be loaded with the Resources.Load function. The second error happens because of the first one, so ignore that for now.

you can upvote comments as well

the name of folder and the asset is written correctly...

and my txt asset is also in the folder Resources under the asset folder

@zohra You are saying that the name of the folder and asset is written correctly AND that the text asset is in the Resources folder. I am slightly confused so I am just asking to clarify: Is your asset in the root of the Resources folder or inside another folder that is in the Resources folder?

1 Answer

1

In addition to the answer by @GameVortex, I’d like to point out that the “as” operator will return null if the object is not of the type specified (TextAsset).

So, either the resource could not be loaded because Resources.Load returned null, or "as TextAsset " returned null.

In either case, you want to add a check for null before referencing MyTextAsset on line 17, which is a good practice in general.