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 Nik · Feb 25, 2011 at 08:05 AM · if-statementsfor-loop

If statement within the For-Loop is not working - Puzzled!

Hi all again,

I am having some puzzling issue with a If-statement within a For-loop. In brief, the script is supposed to, in the beginning, read a txt file into an array, and subsequently the If-statement-within-For-loop will identify the exact text needed and returns the value.

The txt file, named Language.txt, goes like this:

<English>
<Chinese>
<Malay>
<Tamil>

The script that I crafted goes like this:

function Start () { var fileName = "Language.txt"; var sr = new StreamReader(Application.dataPath + "/" + fileName); var fileContents = sr.ReadToEnd(); sr.Close(); var Languages = fileContents.Split("\n"[0]);

 var textNeeded = "&lt;Malay&gt;";
 for (var j : int = 0; j &lt; Languages.Length; j++) {
     if (Languages[j] == textNeeded) {
         print(textNeeded);
     }
 }

}

Up till the part for the loop, the script has no problem, as if I insert a print(j) before the if-statement, all the j values (from 0 to 3) were returned in the debug console. And if I do print(Languages[j]) before (or after) the If-statement, all the Languages[j] are also returned. The one that is not working is the if-statement. Somehow it is not being activated (for lack of a better word).

I have look thru a number of questions/answers here in UnityAnswer, and it seems the script above is not too different from some of these questions/answers (e.g. like this, and theirs are working.). Am I missing something here?

Thanks in advance!

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 · Feb 25, 2011 at 08:14 AM 0
Share

I'm not totally familiar with the StreamReader Class - is .Split() going to return an array of strings? You may also wish to explicitly declare your types. I suspect if you change your ifcheck to "`if (Languages[j] != textNeeded)`", it will print(textNeeded) four times, and it's actually that the variables aren't equivalent, rather than the ifcheck failing.

avatar image efge · Feb 25, 2011 at 08:27 AM 0
Share

I would also suggest to explicitly declare your types for variables.

avatar image Nik · Feb 25, 2011 at 08:43 AM 0
Share

Hi $$anonymous$$arowi and efge, thanks for the quick response. Indeed I think you guys had hit the jackpot. When I change the ifcheck to '!=' ins$$anonymous$$d of '==', I do get four print(textNeeded). I took your advices and tried declaring the 2 variables, for quote: "var Languages : String[] = fileContents.Split("\n"[0]);" unquoted, and quote "var textNeeded : String = "<$$anonymous$$alay>";" unquoted, to explicitly make both variables as String. But it is still working if the ifcheck is "==". Is String[] not the same as String? Thanks!

avatar image Eric5h5 · Feb 25, 2011 at 08:59 AM 0
Share

@$$anonymous$$arowi, efge: neither C# nor JS require explicitly declaring types. Type inference takes care of it. Writing out the type changes exactly nothing.

avatar image Eric5h5 · Feb 25, 2011 at 09:02 AM 0
Share
@Nik: print out each line, so you can see what you're actually getting.
Show more comments

1 Reply

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

Answer by David 3 · Feb 25, 2011 at 10:07 AM

my guess is that your split function is leaving some white space characters before or after each element of Languages.

try:

var textNeeded = "<Malay>";    
 for (var j : int = 0; j < Languages.Length; j++) { 
      if (Languages[j].Trim() == textNeeded) {            
      print(textNeeded);       
   }  else {
  print("notmatched:'"+Languages[j].Trim()+"'");
  } 

}

if it doesnt work let us know what it prints

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 Nik · Feb 25, 2011 at 11:09 AM 0
Share

Hi $$anonymous$$. You are spotted on. From the print(), the outputs were not obvious that there were "space(s)" somewhere before or after the elements (I tried "highlighting" over the print() but didn't see anything like spaces). The use of .Trim() solved this, and this script is now working. Adding on, both Languages[j] and textNeeded are implicitly declared as string, hence this is not a variable mis-match issue. Apprieciated your insight and many thanks!

avatar image David 3 · Feb 25, 2011 at 11:38 AM 0
Share

no worries, thanks for the accept :)

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

No one has followed this question yet.

Related Questions

Question on Possible FOR Loop Condition Conflict 0 Answers

if statement not returning true 1 Answer

Create DoTween from for loop? 1 Answer

Using i in if statement 3 Answers

For Loop Doesn't Appear to be running 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