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 kmccmk9 · Feb 07, 2013 at 04:17 AM · c#variablefalse

Variable is always false...

Hello, so I have this code which works except no matter where I put login = true, it is always false. Why is this? It used to work and I changed something and now it no longer works. It seems that it is checking the variable before the coroutine gets to the point of setting the variable. Any help would be appreciated.

Code:

 using UnityEngine;
 
 using System.Collections;
 
 
 
 public class Login : MonoBehaviour
 
 {
 
 
 
     public Texture LoginBackground;
 
     public Texture2D stylebackground;
 
     public GUIStyle LoginStyle;
 
     public GUIStyle LoginTextBox;
 
     public GUIStyle LoginButton;
 
     public string Username;
 
     public string Password;
 
     public string EncPassword;
 
     public float transparent;
 
     private string url;
 
     public WWW w;
 
     public WWWForm loginform;
 
     public bool doWindow0 = false;
 
     public string formText="";
 
     public bool login;
 
     // Use this for initialization
 
     void Start ()
 
     {
 
         LoginStyle.fontSize = 72;
 
         LoginStyle.alignment = TextAnchor.MiddleCenter;
 
         LoginTextBox.fontSize = 20;
 
         LoginTextBox.alignment = TextAnchor.MiddleCenter;
 
         LoginTextBox.normal.background = stylebackground;
 
         LoginButton.fontSize = 30;
 
         LoginButton.alignment = TextAnchor.MiddleCenter;
 
         loginform = new WWWForm ();
 
     }
 
 
 
     // Update is called once per frame
 
     void Update ()
 
     {
 
         
 
     }
 
 
 
     void OnGUI ()
 
     {
 
         GUI.backgroundColor = Color.black;
 
         GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), LoginBackground, ScaleMode.StretchToFill, false, 0.0f);
 
         GUI.Label (new Rect (Screen.width / 2 - 250, Screen.height / 2 - 250, 500, 250), "Username:", LoginStyle);
 
         Username = GUI.TextField (new Rect (Screen.width / 2 - 250, Screen.height / 2 - 80, 500, 50), Username, 10, LoginTextBox);
 
         GUI.Label (new Rect (Screen.width / 2 - 250, Screen.height / 2 - 50, 500, 250), "Password:", LoginStyle);
 
         Password = GUI.TextField (new Rect (Screen.width / 2 - 250, Screen.height / 2 + 120, 500, 50), Password, 10, LoginTextBox);
 
         if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 200, 300, 50), "Login:", LoginButton)) {
 
             //Encrypt
 
             EncPassword = EncryptData.Encrypt(Username,Password);
 
             //CheckLogin
 
             StartCoroutine(CheckLogin());
 
             doWindow0 = true;
 
         }
 
         if (doWindow0 == true) {
 
             GUI.Window (0, new Rect (Screen.width / 2 - 700, Screen.height / 2 - 100, 400, 120), DoMyWindow, "Notice:");
 
         }
 
         
 
     }
 
 
 
     void DoMyWindow (int windowID)
 
     {
 
         GUI.Label (new Rect (10, 20, 800, 40), "Login success. Click the close button to continue to the character selection screen.");
 
         if (GUI.Button (new Rect (10, 80, 100, 20), "Close")) {
 
             print("login is " + login);
 
             if(login==true)
 
                 Application.LoadLevel ("CharacterSelect");
 
             doWindow0 = false;
 
         }
 
     }
 
 
 
     IEnumerator CheckLogin ()
 
     {
 
         url = "http://redlightlife.tk/scripts/checklogin.php?username=" + WWW.EscapeURL (Username) + "&password=" + WWW.EscapeURL (EncPassword);
 
         w = new WWW (url);
 
         yield return w;
 
         if (w.error != null) {
 
             print (w.error);
 
         }
 
         if (w.error == null) {
 
             formText = w.text;
 
             w.Dispose ();
 
             formText.Trim();
 
             if (formText == "true")
 
             {
 
                 login = true;
 
             }
 
             else if(formText == "false")
 
             {
 
                 login = false;
 
             }
 
             StopAllCoroutines();
 
         }
 
     }
 
 }
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

1 Reply

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

Answer by golden pouria · Feb 07, 2013 at 04:44 AM

There are different possiblities:

1- In the line number 164 you defined:

  formText = w.text;

which is the actual value that you compare in your IF statements in the lines 169 and 177. The value of formText defines whether or not the "login" is "True" or "False". Please note that in lines 169 and 177 you are comparing a string value, and not a Boolean value. So, it could be that you get "True", instead of "true" and you have to see if that makes any difference.

I would suggest that you debug your code by putting the following code at line 168, to see what is the real value in different cases. It will print whatever that the value of the formText is:

 print(formText);

Then you will see it and you can find out why it doesn't return what you want in the IF statements.

PLEASE NOTE:

in line 161, where you check for

    if (w.error == null) 

if the statment returns false, then you would never get to any of the IF statemtnes in the above. So I would also put a

 print (w.error);

too before 161 to make sure.

Have fun!

Comment
Add comment · Show 4 · 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 kmccmk9 · Feb 07, 2013 at 05:02 AM 0
Share

Hi thanks for the reply. First, I'm not sure what line numbers you are referring to as they are not in the code. Also, if w.error is not null it is already set to print the error. Now I have placed sever print(formText) in a few places. Anytime inside of the co-routine it returns a value, anywhere else, it returns a blank. It is like it's reading the value before it gets set in the co-routine.

avatar image golden pouria · Feb 07, 2013 at 05:10 AM 0
Share

By line numbers, I mean in your post, when you post your question, this website automatically puts a line number next to each line of code (5,10,15,...)

Also if what do you mean by "Anytime inside of the co-routine it returns a value". What value does it return? You are checking for 2 string values "true" and "false". anything other than these 2 values don't work for you. In other words, does it return the word "true" (of type string)? If yes, then you should expect the "login" to be true. Other than that it will be always false.

Hope it works.

avatar image kmccmk9 · Feb 07, 2013 at 05:14 AM 0
Share

well when I check formText it is true and login is also true. Only when in the routine though. Everywhere else they're empty. Also the line numbers you mentioned don't exist. It only goes up to 91.

avatar image kmccmk9 · Feb 09, 2013 at 01:02 AM 0
Share

Hi, so I added the print error line and it returned with Null. At the line you line you specified I added the print formText and it returned ( true). The problem is, there is a space. However I am not sure why since I called the trim function before hand. Up in the OnGUI, I added print(formText) again right after coroutine and it returned false.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need to use 2 different language scripts. 1 Answer

Basic C# Public Variable Help 3 Answers

Make an object respond to a click in c# only when a variable equals a certain value 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