Search Unity

What is the problem? C#

Discussion in 'Scripting' started by aixaix, Aug 27, 2012.

  1. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    I have a script that saves and loads a .data file...
    but it doesn't seem to work properly..

    Cause as i press login it says the password is wrong and even if i enter the correct password...

    Code (csharp):
    1.         if(load == true) {
    2.             if(File.Exists(path + enteredUsername + ".data")) {
    3.                 using(FileStream fs = File.OpenRead(path + enteredUsername + ".data")) {
    4.                     byte[] b = new byte[1024];
    5.                     UTF8Encoding temp = new UTF8Encoding(true);
    6.                     while(fs.Read(b,0,b.Length) > 0) {
    7.                         tempPassword = temp.GetString(b);
    8.                     }
    9.                     if(enteredPassword == tempPassword) {
    10.                         LoggedIn();
    11.                     }
    12.                    
    13.                     else {
    14.                         lastMessage = "Wrong password!";
    15.                         load = false;
    16.                     }
    17.                 }
    18.             }
    19.             else {
    20.                 lastMessage = "This account doesn't excist in our database!";
    21.             }
    22.         }
    23.  
     
  2. rob4097

    rob4097

    Joined:
    May 15, 2012
    Posts:
    50
    Just a wild crack for brainstorming purposes - this may be way off base, but is "path" set anywhere?

    Code (csharp):
    1.  
    2. using(FileStream fs = File.OpenRead(path + enteredUsername + ".data")) {
    3.  
    If not, this:

    Code (csharp):
    1.  
    2. while(fs.Read(b,0,b.Length) > 0) {
    3.                         tempPassword = temp.GetString(b);
    4.                     }
    5.  
    would never set "tempPassword", which would cause this:

    Code (csharp):
    1.  
    2. if(enteredPassword == tempPassword) {
    3.                         LoggedIn();
    4.                     }
    5.  
    to always be not true.
     
    Last edited: Aug 27, 2012
  3. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    Yes, i've set the path in the start function.
     
  4. jheiling

    jheiling

    Joined:
    Sep 28, 2010
    Posts:
    65
    Are you missing "/" between path and enteredUsername?
     
  5. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    No the path is alright, cause when i save the file trough script it saves correctly into correct folder,
     
  6. jheiling

    jheiling

    Joined:
    Sep 28, 2010
    Posts:
    65
  7. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    That code is pretty simple. Use Debug.Log to find out where things are going wrong.
     
  8. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Try logging the temp password and see if it matches what you're expecting.
     
  9. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    Logging seemed to help a little, now i know that the tempPassword is what it should be but the script just stops after loading the file...
    Maybe i'll try to call a function where it sets the tempPassword...
     
  10. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    nothing... I't like skips the if(tempPassword == enteredPassword) part :(
     
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Do you have the enteredPassword password at this point in the script, and is it the proper type? You seem to have verified the tempPassword. If you know that is correct, check and see what you are comparing it against. (enteredPassword).

    Toss in a debug before the comparison to output what enteredPassword is and its type.

    You might also double check that you aren't getting white space that is causing the false result. I have ran into that before, and since it does really appear in the debug log, I looking other places. In other words "foo" != "foo ".

    Good luck!
    ZG
     
  12. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    Well, i just debugged both like this
    Code (csharp):
    1.  
    2. Debug.Log("1st: " + tempPassword + ".....");
    3. Debug.Log("2nd: " + enteredPassword + ".....");
    4.  
    but the console shows
    1st: 2
    UnityEngine.Debug:Log(Object)
    Networking:OnGUI() (at Assets/__GData/Scripts/Network/Networking.cs:56)

    2nd: 2.....
    UnityEngine.Debug:Log(Object)
    Networking:OnGUI() (at Assets/__GData/Scripts/Network/Networking.cs:57)

    there's something wrong with tempPassword?
     
  13. aixaix

    aixaix

    Joined:
    May 7, 2010
    Posts:
    523
    Fixed this, thanks to all of you, I've finally figured it out.
    the problem was that when I opened the file it was making the password 1024 characters long so, just replaced:

    byte[] b = new byte[1024];

    with this:

    byte[] b = new byte[enteredPassword.Length];