- Home /
Question by
lilchiker · Nov 30, 2020 at 03:06 PM ·
dictionary
I've been trying to have a dictionary look up for a word entered by the player. The words.ContainKey(word) is always returning false, even when it should return true.
using System.Collections.Generic; using UnityEngine;
public class DictLoader : MonoBehaviour { Dictionary words = new Dictionary ();
public void Awake()
{
WordDictionary dictionary = JsonUtility.FromJson<WordDictionary>(JsonFileReader.LoadJsonAsResource("EnglishDictionary.json"));
Word[] wordList = dictionary.words;
foreach (Word w in wordList)
{
//add all of the words.
if (words.ContainsKey(w.word)){
words.Add(w.word, w);
}
}
}
public void TestWord(string word)
{
if(HasWord(word))
{
Word w = GetWord(word);
Debug.Log(w.pos);
Debug.Log(w.word);
Debug.Log(w.definitions[0]);
}
else
{
Debug.Log("Word not found.");
}
}
public bool HasWord(string word)
{
if(words.ContainsKey(word))
{
return true;
}
return false;
}
public Word GetWord(string word)
{
return words[word];
}
} ,
Comment
Your answer
Follow this Question
Related Questions
typedefing in unity 1 Answer
Need help with targetting 1 Answer
adding dictionary in the game 0 Answers