- Home /
How to make this loop work? freezing on me
I'm trying to make a login screen for my game. cant get this loop to work though. just trying to check to see if the username matches the one that was inputted with the userNameCheck variable.
Currently when i run the loop the game freezes. so im thinking the null isn't working somehow.
can someone help? thanks. here is an image of the login screen:
void CheckForUser()
{
do
{
userNameCheck = File.ReadAllText(path);
if (userNameCheck == userNameText)
{
this.FoundUser();
}
}
while (userNameCheck != null);
//user dosent exist
UnityEngine.Debug.Log ("Did not find user");
}
void FoundUser()
{
UnityEngine.Debug.Log ("Found User");
}
will contain many. but if its the first time someone runs the game it wont contain any users yet.
Hi SpaceSocks, 1. File.ReadAllText(path) returns the string in that path, so to read the text existed in that path you don't need to use any loop.If you are splitting the usernames when you are adding new name every time, you can find the user name(It totally depends upon how you are managing the user names in your text file). 2. If you are splitting the user names with any special character you can find the user name by using one of string functions .contains(username+"special character").
Answer by KellyThomas · May 07, 2014 at 06:14 AM
This looks like an infinite loop.
If this method:
is called when
userNameCheck
holds a non-`null` valueand
File.ReadAllText(path)
returns a non-`null` valueand
this.FoundUser()
never changesuserNameCheck
to anull
value
Then it will never exit the do-while loop
hmm well. you get what im trying to do right? im just trying to get it to go threw the text file line by line. each line will be a stored username so im just trying to do:
if userNameCheck = "what the player typed for username" then goto FoundUser
else do the loop again until it reaches the end of the file. then ill know the user does not exist yet.
O$$anonymous$$... Then File.ReadAllText()
is not the method you are looking for.
You might want to try File.ReadLines()
ins$$anonymous$$d (no need for the while loop):
foreach(string line in File.ReadLines(path)){
if(line.Trim() == userNameText) {
this.FoundUser();
return;
}
}
Answer by GenuiTix · May 07, 2014 at 06:05 AM
I guess you meant to restart loop if userNameCheck is null. Then:
while (userNameCheck == null);
no just the opposite. im trying to get it to run though the loop until EOF.
Answer by wibble82 · May 07, 2014 at 06:44 AM
Hi
I guess what you're trying to do is read the file 1 line at a time, because each line contains a user name. However your code is simply reading the whole file into a string every iteration of the loop, so it'll never end up being null if the file exists.
Check out this link for reading a text file 1 line t a time:
http://msdn.microsoft.com/en-GB/library/aa287535(v=vs.71).aspx
Your answer
Follow this Question
Related Questions
Making A 4 Barrel ShotGun! Need Some Help - UPDATE 3 Answers
Do-While loop 2 Answers
foreach iteration variable.. null? 1 Answer
or doesn't work in while loops? 1 Answer
Decrease a value every second? 3 Answers