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 jach5270 · Jul 10, 2014 at 12:35 AM · textimportstringsimplereading

(FIXED) TextAsset.name is never equal to compared string

so here's the problem. i'm trying to load text assets based on a list of file names (in this case its simple config files for weapons). first off i was loading them from an external file using a streamReader, which was working perfectly and still does. now, i'm trying to do the same thing using textAssets imported into unity. the problem is that when i try to compare the 'name' of some of the textAssets to the list of names that i have, they never return true. no matter what.

i've been pulling my hair out for hours over a simple: is "weapon" == "weapon"? nope. i've debugged the hell out of it and the strings i am trying to compare are 100% equal, yet they never return true.

here is part of my code:

 for (int a = 0; a < SaveManager.defaultSaves.Length; a++){
     Debug.Log(SaveManager.defaultSaves[a].name + "_" + fileName);
     if(SaveManager.defaultSaves[a].name == fileName){

alt text

there are definitely no extra spaces or anything like that. this is driving me insane. worst part is that it actually does work in other scenarios. just not this one for god knows what reason.

i can't even really think of what an answer to this would even be, there seems to be no reason as to why these aren't working.

here's some more of the code:

     private static bool Load(string fileName, bool useDefault){
         if(!useDefault){
             try{
                 string line;
                 string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                 string file = dir + @"\data\" + fileName + ".txt";
                 StreamReader theReader = new StreamReader(file, Encoding.Default);
 
                 using (theReader){
                     do{
                         line = theReader.ReadLine();
                         
                         if (line != null){
                             if(line.Length != 0){
                                 if(line[0] != '/')
                                     SaveManager.data.Add(line);
                             }
                         }
                     }
                     while (line != null);
                     theReader.Close();
                     return true;
                 }
             }
 
             catch (IOException e){
                 Debug.Log("" + e.Message);
                 return false;
             }
         }
         else{
             for (int a = 0; a < SaveManager.defaultSaves.Length; a++){
                 Debug.Log(SaveManager.defaultSaves[a].name + "_" + fileName);
                 if(SaveManager.defaultSaves[a].name.ToString() == fileName.ToString()){
                     string[] lines = SaveManager.defaultSaves[a].text.Split("\n"[0]);
                     for (int i = 0; i < lines.Length; i++){
                         if (lines[i] != null){
                             if(lines[i].Length != 0){
                                 if(lines[i][0] != '/')
                                     SaveManager.data.Add(lines[i]);
                             }
                         }
                     }
                     return true;
                 }
             }
             Debug.Log("file not found: " + fileName);
             return false;
         }
     }


FIXED!

i added a new line of code:

 lines[i] = lines[i].Trim();

turns out it problem was caused \r characters. using Trim() removed any \r characters and fixed the issue.

Comment
Add comment · Show 8
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 meat5000 ♦ · Jul 09, 2014 at 11:50 PM 0
Share

Use .ToString() for each item you want a String for, if it doesn't already give you one.

avatar image jach5270 · Jul 10, 2014 at 12:20 AM 0
Share

adding ToString() didn't make a difference. i even tried adding (string) before each variable and even breaking them both down into an array or chars and back to strings again.

also just to clarify, in the example 'defaultSaves' is an array of textAssets, and 'fileName' is a string parameter for the function this is in.

avatar image meat5000 ♦ · Jul 10, 2014 at 12:36 AM 0
Share

Debug.Log inside a for doesn't really work and will only seem to kick out one result. You can't rely on it to debug a for loop. What comes after your if statement?

avatar image jach5270 · Jul 10, 2014 at 12:52 AM 0
Share

i updated the question with a bit more code.

avatar image robertbu · Jul 10, 2014 at 01:02 AM 1
Share

Not sure if this is the issue, but you are not stripping out carriage returns. You are only stripping out newlines. So depending on how the text was authored, you may have a hidden character in each string. Try splitting on both '\r' and '\n' and discarding empty lines (StringSplitOptions.RemoveEmptyEntries).

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jach5270 · Jul 10, 2014 at 01:16 AM

@robertbu thank you so much! it worked! i looked up on '\r' usage and found the Trim() method, and then added a new line of code: lines[i] = lines[i].Trim(); and now it works!

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

6 People are following this question.

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

Related Questions

Looking for a javascript unity implementation of FromCharCode? 0 Answers

Separating two strings 1 Answer

Need help (String) 2 Answers

Reading integer value from specified character(s) on text line? 2 Answers

NEW GUI SYSTEM - text with multiple values. 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