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
0
Question by nomadic · Nov 20, 2011 at 04:52 AM · javascript.txt

FormatException: Input string was not in the correct format

Hello, I'm trying to read in a series of .txt files from my Resources folder which will generate a tile map. So far, I've been able to parse a single txt file after linking it in the inspector. However, when I try to read in a .txt with Resources.Load, the supposedly same string data cannot be parsed to integers without resulting in the error stated in my the title of this thread.


My code:

 var mapList : Object[];
 var levelIndex : int;
 var numLevels : int;
 var mapAsset : TextAsset;
 var mapString : String;
 
 function Start () {
 
     mapList = Resources.LoadAll("maps", TextAsset);
     
     levelIndex = 0;
     GenerateMap();
 }
 
 function GenerateMap () {
     
     // Grab txt from list of maps
     mapAsset = mapList[levelIndex];
     
     // Read in level text
     mapString = mapAsset.text;
     var mapData : Array = mapString.Split(","[0]);
     var index : int = 0;
     for (var h : int = 0; h<widthInTiles; h++) {
         for (var w : int = 0; w<widthInTiles; w++) {
             print(mapData[0]);
             SpawnTile(w, -h, parseInt(mapData[index]));   <<<--- Error
             index++;
         }    
     }
 }


In summary:

  • My .txt file is just several lines of numbers separated by commas ("0,0,1,2,3,0" etc.)

  • parseInt() works fine when I manually reference mapAsset in the Inspector.

  • If I do print(mapData[0]) where I indicate it prints "0", and printing mapString prints the complete text file as it should.

So.. What the heck is going on?

Comment
Add comment
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

2 Replies

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

Answer by DaveA · Nov 20, 2011 at 04:57 AM

Not sure if this will help but try:

 var mapData : String[] = mapString......


Also use the debugger and/or Debug.Log to print the elements of mapData to see what they look like.

Comment
Add comment · Show 7 · 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 Eric5h5 · Nov 20, 2011 at 05:11 AM 0
Share

Yes, you almost never want to use Array. Split returns String[], no point casting that to Array.

avatar image nomadic · Nov 20, 2011 at 05:13 AM 0
Share

Thanks for the quick reply, Dave. Swapping "String[]" for "Array" didn't fix the error, but the error was slightly different. $$anonymous$$aybe it sheds some light on the problem:

"FormatException: Input string was not in the correct format System.Int32.Parse (System.String s)" vs. "FormatException: Input string was not in the correct format int.Parse (string) "

avatar image nomadic · Nov 20, 2011 at 05:14 AM 0
Share

Also, how can I go about using Debug.Log to get more useful info? Sorry still pretty new to program$$anonymous$$g in general.

avatar image Eric5h5 · Nov 20, 2011 at 05:17 AM 0
Share

print (mapData[index]); rather than just 0. Although putting Debug.Log in inner loops can be a problem since it's very slow, so you might want to limit the output in some way. Debug.Log = print.

avatar image nomadic · Nov 20, 2011 at 05:34 AM 0
Share

Thanks, Eric, I should have done that right off the bat. It was actually just a formatting error with one of my .txt files. There were no commas at the end of each line, so what looked like parseInt("00") was actually "0*linebreak*0". And thanks for the tip, Dave.

Show more comments
avatar image
0

Answer by nomadic · Nov 22, 2011 at 01:14 AM

*** "`\n` and \r"

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with eval() 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

About "translating" js into C# 1 Answer

FindIndex in a List 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