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 aabattery · Sep 10, 2012 at 09:08 PM · wwwstringif statementcompare

If Statement Acting Strange

I'm trying to make a game in which you need to enter a code to get items you want. I have all the codes in a SQL table on my localhost and I'm using WWW and PHP to reach it. I'm also using String.Compare to compare a string received from the PHP script to a word or phrase. The errorMsg variable changes the value of a GUI Label which displays when the variable currentWindow is equal to Window.Failed. Here's the function that controls that:

 function RegisterItem(code : String) {
         registerURL = registerURL + "code=" + WWW.EscapeURL(code) + "&version=" + game.version;
      
         // Post the URL to the site and create a download object to get the result.
         var code_post = WWW(registerURL);
         currentWindow = Window.Downloading;
         yield code_post; // Wait until the download is done
         if(code_post.error) {
             errorMsg = code_post.error;
             currentWindow = Window.NoConnection;
         } else {
             if(String.Compare(code_post.text, "Used", true) > 0){
                 errorMsg = "It seems that this code has already been used...";
                 currentWindow = Window.Failed;
                 Debug.Log(code_post.text);
             } else if(String.Compare(code_post.text, "No code", true) > 0){
                 errorMsg = "This code is not in our database.";
                 currentWindow = Window.Failed;
                 Debug.Log(code_post.text);
             } else if(String.Compare(code_post.text, "Version", true) > 0){
                 errorMsg = "Download the latest version to register this item.";
                 currentWindow = Window.Failed;
                 Debug.Log(code_post.text);
             } else if(PlayerPrefs.HasKey(code_post.text)) {
                 errorMsg = "It seems that you already own this item.";
                 currentWindow = Window.Failed;
                 Debug.Log(code_post.text);
             } else {
                 game.item = code_post.text;
                 game.items.Push(game.item);
                 currentWindow = Window.CodeSuccess;
             }
         }
     }

I have the Debug Log print out what the value of code_post.text is to see what it is. When I test run the game and use a fake code I entered into the SQL table, I see that in the Debug Log, it prints out "Version", yet the GUI Label shows the phrase I put if code_post.text is equal to "Used". It doesn't seem to be a problem with the PHP script (although it shouldn't be saying that the version is wrong, but I'm not focused on that right now). What could possibly be the problem?

Comment
Add comment · Show 2
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 Bunny83 · Sep 10, 2012 at 09:36 PM 0
Share

Well, where do you use "error$$anonymous$$sg"? Are you sure that you access the right variable? I guess "currentWindow" is an enum which is used to display different "windows" ?

I can't see any obvious error in your code, maybe you can include the part where error$$anonymous$$sg is used?

avatar image aabattery · Sep 10, 2012 at 09:42 PM 0
Share

Like I said, error$$anonymous$$sg is being used in a GUI Label. All it is is:

case Window.Failed: GUILayout.Label(error$$anonymous$$sg); if(GUILayout.Button("Okay")) { currentWindow = Window.Register; } break;

It is obviously in a switch inside the OnGUI function.

1 Reply

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

Answer by Bunny83 · Sep 10, 2012 at 10:07 PM

Well, i just realized you used String.Compare which returns "an integer that indicates their relative position in the sort order".

It should be 0 when they are equal since than they have the same "level" in the sort order.

You might want to use String.Equals instead and use the StringComparison value of CurrentCultureIgnoreCase for example.

 if (code_post.text.Equals("No code", System.StringComparison.CurrentCultureIgnoreCase))


usually it's simpler to just convert the string you want to test to lower-case and do an ordinary string comparison:

 var code = code_post.text.ToLower();
 
 if (code == "no code")
 //[...]

Keep in mind that your "constants" need to be lower-case

Comment
Add comment · Show 1 · 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 aabattery · Sep 10, 2012 at 11:21 PM 0
Share

Oh...! I thought that it wasn't equal if it was 0... Thanks. :)

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

9 People are following this question.

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

Related Questions

www.data and string conversion to integer 2 Answers

How to convert a string to a type WWW 0 Answers

How do I find a String in an Array? 2 Answers

Tag comparision with a string as variable doesnt work 4 Answers

Getting String From WWW 2 Answers


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