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 Cataclysm Entertainment · May 23, 2014 at 11:42 AM · arraysstringloop

Array strings/Loops problem

HI everyone, I've a problem, why Unity do not recognize all strings of my array or just the last.... Here is my code :`

 var cheatField : String = "Cheat code";
 var cheatCodes : String[];
 
 if (GUI.Button(Rect(Screen.width - Screen.width, Screen.height - 60, 250, 20), "Try debugging")) {
     
         for (var i : int = 0; i < cheatCodes.Length; i++) {
         
                 //for (var code : String in cheatCodes) {
     
                     if (cheatField == cheatCodes[i]) {
             
                         msgInfos = "Code recognized";
                     
                     }
         
                     if (cheatField != cheatCodes[i]) {
             
                         msgInfos = "Error : unknown code !";
 
                     }
     
         }`

thanks for advance

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
0
Best Answer

Answer by Fornoreason1000 · May 23, 2014 at 12:02 PM

what happening your code is running for each cheat code inside you list, try debugging you msgInfos and both of them came up regardless...the problem was that even after you found a match or an error, your looping code would continue to run causing you problems, Unity reconied all your strings all along, its was just made hard due to a tiny logical mistake

 #pragma strict
 
 
 var cheatField : String = "Cheat code";
 var cheatCodes : String[];
 var msgInfos : String;
 
 
 function OnGUI() {
     if (GUI.Button(Rect(Screen.width - Screen.width, Screen.height - 60, 250, 20), "Try debugging")) {
  
 
         var match = false;
         for (var i : int = 0; i < cheatCodes.Length; i++) {
  
             //for (var code : String in cheatCodes) {
  
                if (cheatField.ToUpper() == cheatCodes[i].ToUpper()) {
  
                    match = true;
                   msgInfos = "Code recognized";
                   Debug.Log(msgInfos);
                     }
                 }
 
     if (match == false) {
  
         msgInfos = "Error : unknown code !";
         Debug.Log(msgInfos);
     }
     match = false;
  
 
     }
 }

I have edited your code, my Unity Script is poorly formatted i apologies. to fix your issue, i moved your Error Message outside of the loop then i changed the condition to check for a bool called watches was false, i then set it to true whenever a reconised string was put through and done!

Debug.Log is a very useful function and lets you know everything that goes on if you use it right

Hope it helps

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 Cataclysm Entertainment · May 23, 2014 at 03:00 PM 1
Share

Thanks Fornoreason1000 for your great answer, now I understand. $$anonymous$$y script was for creating GUI Textfield for in game cheat codes. I hope also that this question help others unity developpers. Thabks again for all.

avatar image Fattie · May 24, 2014 at 07:47 AM 1
Share

IF THAT'S THE ANSWER, TIC$$anonymous$$ IT AND VOTE FOR IT

avatar image
1

Answer by Asteiros · May 23, 2014 at 12:06 PM

You are setting value msgInfos in a loop. As a result you'll get "Code recognized" if last one is "Cheat code" else "Error...". Use msgInfos += "Code recognized\n".

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using LINQ within a foreach only runs once 1 Answer

how do I scroll thru a array 2 Answers

Assets/Scripts/Enemy.cs(61,44): error CS1955: The member `UnityEngine.GameObject.tag' cannot be used as method or delegate 1 Answer

How to split a string into array? 2 Answers

public string array controlled by an int in the inspector 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