Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dav78 · Jun 18, 2013 at 02:20 AM · javascripttextdictionary

Javascript TextAsset to Generic Dictionary

I have read around and it seems the best way to use a text file like a hashtable is with a generic dictionary. Of course, easier said than done. There are plenty of examples on parsing text files, but none on passing them into a dictionary that I have found.
I have cobbled together some code from the limited information/examples that exist on Javascript dictionaries. It doesn't work , but I'm not sure why. I can get the code to compile without errors, so I assume the syntax is OK(if conceptually incorrect/incomplete), but it fails to find the key/value pairs in the dictionary. Error message is

KeyNotFoundException: The given key was not present in the dictionary.

I suspect there might be two errors: one in how I understand splitting the text file into arrays, and another in how I understand adding/fetching key/value pairs in a dictionary.

I am a novice, so please excuse some of my ignorance. Any advice would be appreciated. Code is below:

Text file is just this:

 NUMA*One
 NUMB*Two
 NUMC*Three
 NUMD*4
 NUME*Five
 NUMF*Six
 NUMG*7
 NUMH*Eight

Code attached to GameObject is:

 #pragma strict
 import System.Collections.Generic;
 
 /* Load the text file */
 var textFile : TextAsset;
 /* Create an dictionary for the values in the file */
 var textInfo = new Dictionary.<String,String>();
 
 
 function Start () {
 
     /* split the text file up by newline characters */
     
     var textLines : String [] = textFile.text.Split("/n"[0]);
 
     /* loop through each line in the file */
  
     for ( var thisLine : String in textLines ) {
  
     /* split each line by asterisks into array of two variables*/
  
     var valueString : String [] = thisLine.Split("*"[0]);
  
     /* add the two variables in the array into the dictionary as a key and value*/
  
     textInfo.Add(valueString[0], valueString[1]);
     }
 }
 
 
 function Update () {
 
 }
 
 function OnGUI () {
     
     /* make a labelled box to show a string pulled from dictionary by key*/
     GUI.Box (Rect (10,10,80,80), textInfo["NUMB"]);
 }

Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Essential · Aug 19, 2013 at 11:35 PM 0
Share

Thanks for posting your code dav78! I found it really useful so gave you a thumbs up. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Jun 18, 2013 at 03:15 AM

You can do basic debugging using Debug.Log to see what the actual results of your code are compared to what you might expect. Examples are

 Debug.Log (textLines.Length);
 Debug.Log (thisLine);
 Debug.Log (valueString[0] + " " + valueString[1]);

In this case the result of the first one (that the length of the array is 1) indicates that something fundamental is going wrong. A closer look reveals that in fact you are splitting on the "/" character, which doesn't occur in your data file. To indicate a newline character, use "\n".

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dav78 · Jun 18, 2013 at 04:31 AM 0
Share

Haha, thanks so much! I have mixed feelings. On the one hand, I'm happy my basic code structure was correct. On the other hand, it is such a stupid $$anonymous$$or mistake of one character. Anyway, your point about debugging is well made. I should have gone to Debug first. A valuable lesson. Nonetheless, thanks for your time!

avatar image Eric5h5 · Jun 18, 2013 at 04:38 AM 0
Share

it is such a stupid $$anonymous$$or mistake of one character

Welcome to program$$anonymous$$g. ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best Way to Represent an Inventory? 0 Answers

importing english dictionary as text file? 1 Answer

How to get the keys of a SimpleJSON dictionary ? 3 Answers

GUIText Help 1 Answer

UI Text showing weird string value? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges