Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
c# unity pause a for loop untill bool returns true

i have a for loop that loops through a class array instantiating a new player class and passing in a string(name).

public void createPlayers( int pInput)
{
    //holds name
    string temporaryString="defualt";
    players = new Player[pInput];
   

    for (int i = 0; i < pInput; ++i)
    {
       
        //PlayerTypesNameFunction()// this gets and returns a string to Temporary String
        //wait till function returns with true meaning player(i) has entered name

        players[i] = new Player(100,temporaryString);
        Debug.Log(players[i].m_sName);
    } 
        
    CurrentState = GameState.m_eQueueWeather;
    CallGameCurrentState();


}

I want it to call a function which gets the player to type a name and only continue for loop when that function finishes. changes a bool to true(bool gotInfo), the loop then continues to take the string which is returned and stored in temporaryString (string instatiated above) and assigns it to the new player in this line players[i] = new Player(100,temporaryString);.

Am I heading in the right direction or should I approach this problem differently? Thank you in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I would not suggest the infinite loop here. I would go for the yield return subrutine method that's explained here

Create the subrutine

 IEnumerator WaitForKeyDown(KeyCode keyCode)
 {
     while (!Input.GetKeyDown(keyCode))
         yield return null;
 }

Wait wherever you want

 yield return StartCoroutine(WaitForKeyDown(KeyCode.Return));

This will resume when the user presses enter. You can then check the input value and re-ask.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!