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 SpaceSocks · May 07, 2014 at 05:59 AM · loopnullwhile-loop

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:

link text

 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");
     }

 



Comment
Add comment · Show 3
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 Noob_Vulcan · May 07, 2014 at 06:05 AM 0
Share

your text file contains only 1 username? or many?

avatar image SpaceSocks · May 07, 2014 at 06:29 AM 0
Share

will contain many. but if its the first time someone runs the game it wont contain any users yet.

avatar image shanth · May 26, 2014 at 05:40 AM 0
Share

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").

3 Replies

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

Answer by KellyThomas · May 07, 2014 at 06:14 AM

This looks like an infinite loop.

If this method:

  1. is called when userNameCheck holds a non-`null` value

  2. and File.ReadAllText(path) returns a non-`null` value

  3. and this.FoundUser() never changes userNameCheck to a null value

Then it will never exit the do-while loop

Comment
Add comment · Show 9 · 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 SpaceSocks · May 07, 2014 at 06:28 AM 0
Share

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.

avatar image SpaceSocks · May 07, 2014 at 06:31 AM 0
Share

here is an image of what im doing.. link text

avatar image KellyThomas · May 07, 2014 at 06:45 AM 0
Share

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;
         }
     }
avatar image SpaceSocks · May 07, 2014 at 06:49 AM 0
Share

thank you much!

avatar image SpaceSocks · May 07, 2014 at 07:40 AM 1
Share

omg it works! lol thank you so much.

Show more comments
avatar image
0

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);

Do while - C#

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 SpaceSocks · May 07, 2014 at 06:41 AM 0
Share

no just the opposite. im trying to get it to run though the loop until EOF.

avatar image
0

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

Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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