Home > Back-end >  My while function that is in foreach isn't initializing? Unity
My while function that is in foreach isn't initializing? Unity

Time:03-11

Know this code is supposed find me a few positions that are within the given range and place my objects to those places. But it somehow fails to run a simple while function that will find those positions

     void SortCharacters()
{
    int i = 1;

    foreach (Transform child in Center.transform)
    {
        float x = 0;
        float y = 0;
        
        Debug.Log("**********************");

        while (x * x   y * y > 0.4f * 0.4f)
        {
            Debug.Log("-------------------");
            Debug.Log("a: "   x   " b:"   y);
            x = (UnityEngine.Random.Range(0, 8) - 4) / 10;
            y = (UnityEngine.Random.Range(0, 8) - 4) / 10;
        }

        float DistanceRoughly = (i / 6);
        float CalculatedDistanceMultiplier = (float)Math.Round(DistanceRoughly, 0, MidpointRounding.AwayFromZero);

        Vector3 pos = new Vector3(x, y, 0) * CalculatedDistanceMultiplier;

        child.transform.position  = pos;
        i  ;


        Debug.Log("Found Obj: "   child   "   x:"   x   " y:"   y   " Mult:"   CalculatedDistanceMultiplier);
    }
}

so it prints out stars, childs and mult the way it should. But it is saying that my x, y both are 0 AND it does not print "-------------------". P.S: it also does not print the "a: b:" thing

CodePudding user response:

It is because in your while loop you are checking:

0 * 0 0 * 0 > 0.4f * 0.4f which is not correct so it is not going inside of while loop. You set float x = 0 and float y = 0 so it is correctly following the code.

  • Related