- Home /
My if statement won't work
Hello, my if statement won't work. It won't check the if statement. The problem is occurring in the function WhichWindow. Why won't it check the if statement? It always returns false.
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 float transparent;
private string url;
public WWW w;
public WWWForm loginform;
public bool doWindow0 = false;
public string formText;
// 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))
{
//CheckLogin();
StartCoroutine(CheckLogin());
print(WhichWindow(formText));
}
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), formText);
if (GUI.Button (new Rect (10,80,100,20), "Close"))
{
Application.LoadLevel("CharacterSelect");
doWindow0 = false;
}
}
IEnumerator CheckLogin()
{
url = "http://redlightlife.tk/scripts/checklogin.php?username=" + WWW.EscapeURL(Username) + "&password=" + WWW.EscapeURL(Password);
w = new WWW(url);
yield return w;
if (w.error != null)
{
print(w.error);
}
if (w.error == null)
{
formText = w.text;
w.Dispose();
print(formText);
StopAllCoroutines();
}
}
bool WhichWindow (string text)
{
print("WhichWindow function started");
print(text);
if (text == "Login success. Click the close button to continue to the character selection screen.")
{
return true;
}
else
{
return false;
}
}
}
Apparently the string in formText is not exactly equal to "Login success. Click the close button to continue to the character selection screen."
I don't see how it's not though. I copied it directly from the php echo line as well as printed formText to make sure it matches.
You can't escape the obvious. If it never enters the IF block, then obviously the boolean expression isn't resolving to true.
Even if the strings look the same, that doesn't mean that they are identical data sequences. There may be extra white spaces in the text, or different character codes that use similar-looking glyphs. You should exa$$anonymous$$e the raw string data and check both strings side-by-side, and I'll bet the difference will reveal itself immediately.
I coded a simple function that will compare both strings and show the difference between them.
I tested it on your checklogin.php script and discovered the "Login or password can not be empty." message has an extra white space before and after the string that gets downloaded, even though it is printed without white spaces in the HT$$anonymous$$L on the web page. Chances are that's your only problem, you just have to trim the white spaces out (i.e. text.Trim()).
static private bool VerifyStrings(string a, string b)
{
// <BOS> Begining of String
// <EOS> End of String
// These tags will make it possible to see invisible white spaces at the beginning and end of the strings.
for (int i = 0; i < a.Length && i < b.Length; i++)
{
if (a[i] != b[i])
{
string carrot = new string(' ', i + 5) + "^";
Debug.Log("<BOS>" + a + "<EOS>\n" + "<BOS>" + b + "<EOS>\n" + carrot + " $$anonymous$$ismatch!");
return false;
}
}
if (a.Length != b.Length)
{
string carrot = new string(' ', $$anonymous$$ath.$$anonymous$$in(a.Length, b.Length) + 5) + "^";
Debug.Log("<BOS>" + a + "<EOS>\n" + "<BOS>" + b + "<EOS>\n" + carrot + " $$anonymous$$ismatch!");
return false;
}
Debug.Log("<BOS>" + a + "<EOS>\n" + "<BOS>" + b + "<EOS>\n" + "Strings are identical!");
return true;
}
You could use if (a.Trim() == b.Trim()) if you're worried about leading and trailing spaces. Alternatively, if (a.Contains("Success")) might be more robust to changes in the output of the php.
Answer by Akill · Aug 01, 2012 at 11:24 AM
Apart from the fact you should use .Equals() method when comparing equality of strings, and not ==, I can just re-iterate what other people have said.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
if a string has certain letter(s) or number(s) 2 Answers
If statement not working correctly. 3 Answers
if function broken? 3 Answers