Search Unity

Play mode stuck in endless loop

Discussion in 'Editor & General Support' started by RonErez, Sep 25, 2016.

  1. RonErez

    RonErez

    Joined:
    Aug 30, 2016
    Posts:
    13
    when i press play unity gets stuck in an endless loop, and i dont know why.

    Code (CSharp):
    1. void Start () {
    2.         score = 0;
    3.         life = 3;
    4.         currentLevel = 1;
    5.         levelText.text = "Level: " + currentLevel;
    6.         updateScore ();
    7.         gameOver = false;
    8.         restart = false;
    9.         bossLevel = false;
    10.         gameOverText.text = "";
    11.         restartText.text = "";
    12.         do {
    13.             StartCoroutine (SpawnWaves ());
    14.         } while(gameOver == false);
    15.     }lse);
    i change the gameOver bool when the player losses so i dont know why is this an endless loop.
    and it dosent even enter the function SpawnWaves() so i dont know whats happening.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,537
    The formatting looks totally broken, but anyway...

    The while loop has to return something or it will repeat itself in the same frame until the condition is false. Just add `yield return null;` to make it wait until the next frame before looping again.
     
  3. RonErez

    RonErez

    Joined:
    Aug 30, 2016
    Posts:
    13
    whats broken about the formatting? (im new at this)
    and i cant put yield return null in the start function since its void, tried moving it to a function thet returns IEnumator and calling it, it dosent start the coroutine. ideas?
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,537
    It looked like you just pasted the code and the site broke it, but after looking at it I can see you don't know how to program.

    Start with the Learn section of the site and check out beginner C# tutorials or you'll continue to be lost and confused. Research problematic areas and solve them specifically when you run into a wall.

    The fixed code would be something like this
    Code (csharp):
    1.  
    2.         void Start()
    3.         {
    4.             StartCoroutine(SpawnWaves());
    5.         }
    6.  
    7.         IEnumerator SpawnWaves()
    8.         {
    9.             while (!gameOver)
    10.             {
    11.                 // Spawn code
    12.                 // right here
    13.  
    14.                 yield return new WaitForSeconds(TimeBetweenWaves);
    15.             }
    16.         }
    17.  
    18.         void EndGame()
    19.         {
    20.             gameOver = true;
    21.         }
    See, Start() tells the coroutine to run. The coroutine then says "well, if it isn't game over then I should constantly do this chunk of code". That chunk of code always basically says " spawn a monster, then pause this routine some period of time, the amount in that variable TimeBetweenWaves".

    Looks like you kinda coded it logically backwards because you don't understand the syntax of general programming which is a serious issue if you want to get anything done, which is why I recommend backing up and learning the basics first.

    So it safely loops because it has conditions to do so. If there were no `yield return` then it would continue looping in the while loop endlessly in the same frame until the condition changed to false (which is the gameOver boolean).
     
  5. RonErez

    RonErez

    Joined:
    Aug 30, 2016
    Posts:
    13
    im new to programing games (not programing) so the structure of a game is new to me.
    and i already tried what you said , but it caused other problems in the game. in short i need to learn how to code the game flow like how to initiate levels and such. any guides you know? the basic unity projects guide's are quite simple for a simple game, i just cant continue from there.
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,537
    All syntax and principles are the same. Unity has its own MonoBehavior custom methods and their execution order is here. You can reference the Unity API and apply all standard programming principles.

    So, start with the Learn section if you're having trouble, it covers all of the basics.