Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't understand simple while loop example

Discussion in 'Scripting' started by diffeomorph, Feb 4, 2021.

  1. diffeomorph

    diffeomorph

    Joined:
    Jan 14, 2021
    Posts:
    6
    Hello,

    I'm confused about why the following prints "Still alive!" only once. (This is from an example in the book
    Learning C# by Developing Games with Unity 2020.) If I do "Still alive!" + playerLives, it prints multiple times. Can anyone tell me why is this so? Thanks.




    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ch4Script4 : MonoBehaviour
    {

    // Start is called before the first frame update
    void Start()
    {
    int playerLives = 3;

    while (playerLives > 0)
    {
    Debug.Log("Still alive!");
    playerLives--;
    }



    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Most likely you simply have the "Collapse" option toggled on your console window:
    upload_2021-2-4_14-50-11.png
    That option will collapse identical log messages into a single line with the number of copies shown at the right.
     
    Bunny83, Joe-Censored and diffeomorph like this.
  3. diffeomorph

    diffeomorph

    Joined:
    Jan 14, 2021
    Posts:
    6
    Oh my goodness. You are exactly correct. Thank you so much! I have been puzzling over that for hours! (It's pretty embarrassing.)
     
    Bunny83, Joe-Censored and PraetorBlue like this.