Why is this an endles/infinite lopp and how can i fix it?

Unity crashes when I hit play, so I need to close it by Task_Manager. That means in most cases that there is in the code an endless loop.

How can I fix it?

using UnityEngine;

public class Ground : MonoBehaviour
{
    public Rigidbody ground;
    public int scale;
    public float zScale;
    public bool groundisbigenough ;

    void Start()
    {
        while (groundisbigenough = true)
        {
            Rigidbody clone;
            clone = Instantiate(ground, transform.position = 
                new Vector3(0.0f, 0.0f, zScale), transform.rotation);

            scale++;
            zScale++;

            if (scale == 10)
            {
                groundisbigenough = true;
            }
            else
            {
                groundisbigenough = false;
            }
        }
    }
}

2 answers

  • answered 2020-07-29 17:34 php123

    You are assigning a value of true to groundisbigenough at the start of each iteration of your while loop.

    You should instead be checking if the value of groundisbigenough is true like this:while (groundisbigenough == true)

  • answered 2020-07-29 17:40 Rufus L

    The problem is in your while condition:

    while (groundisbigenough = true)
    {
    }
    

    Here, the condition is an assignment, not a comparison. This doesn't cause a compile error, however, because in c# an assignment returns a value (the result of the assignment), and in this case the result of the assignment is a bool, which satisfies the requirement for a while condition.

    Instead, we should use ==, which is a comparison operator:

    while (groundisbigenough == true)
    {
    }
    

    Or, to be more concise, we can leave out the operator altogether since groundisbigenough is alread a bool:

    while (groundisbigenough)
    {
    }
    

    There is another problem, however, which is that the default value for a bool is false, which means that the while loop will never execute.

    And there's still issues with the logic: since groundisbigenough will get set to false inside the loop (and the loop will exit because the while condition will return false) unless scale == 10, and if scale starts at any number other than 9, then when it increments it will not equal 10.

    If you're trying to increase scale as long as groundisbigenough is false, then your code should be refactored to reflect this behavior (please note that I'm not familiar with Unity, so I'm assuming your Unity-specific code is correct):

    public class Ground : MonoBehaviour
    {
        public Rigidbody ground;
        public int scale;
        public float zScale;
        public bool groundisbigenough = false;
    
        void Start()
        {
            while (groundisbigenough == false)
            {
                Rigidbody clone;
                clone = Instantiate(ground, transform.position = 
                    new Vector3(0.0f, 0.0f, zScale), transform.rotation);
    
                scale++;
                zScale++;
    
                if (scale >= 10)
                {
                    groundisbigenough = true;  // This will cause the while loop to exit
                }
            }
        }
    }
    

    This logic can then be simplified to:

    public class Ground : MonoBehaviour
    {
        public Rigidbody ground;
        public int scale;
        public float zScale;
    
        void Start()
        {
            while (scale < 10)
            {
                Rigidbody clone = Instantiate(ground, transform.position = 
                    new Vector3(0.0f, 0.0f, zScale), transform.rotation);
    
                scale++;
                zScale++;
            }
        }
    }
    

    Since I'm not familiar with Unity, that's as far as I can take it, but it does seem like the loop is unnecessary, since it will simply execute the same operation over and over again until scale == 10. Logically, then, this should be equivalent:

    public class Ground : MonoBehaviour
    {
        public Rigidbody ground;
        public int scale;
        public float zScale;
    
        void Start()
        {
            if (scale < 10)
            {
                var difference = 10 - scale;
    
                scale += difference;
                zScale += difference;  // Increment zScale the same amount as scale increments
                
                Instantiate(ground, transform.position = 
                    new Vector3(0.0f, 0.0f, zScale), transform.rotation);
            }
        }
    }