do-While loop going nuts

Hello!

So I have this script for randomly placing objects on my terrain, and I only want objects to be placed if they are not to far up on the terrain.
Here is the code:

		for (int i = 0; i < nrObjectsToPlace; i++) {
			int random = Random.Range(0, gameObjectsToPlace.Length-1);
			currObject = Instantiate(gameObjectsToPlace[random].gameObject) as GameObject;

			do{
			randomx = Random.Range(thisPos.x, sizeToPlaceOverX);
			randomy = Random.Range(thisPos.y, sizeToPlaceOverZ); //spelar nog ingen roll
			randomz = Random.Range(thisPos.z, sizeToPlaceOverZ);
			
			float normX = randomx * 1.0f / (terrainData.alphamapWidth - 1);
			float normZ = randomy * 1.0f / (terrainData.alphamapHeight - 1);
			
			currAngle = terrainData.GetSteepness(normX, normZ);

			currPos = new Vector3(randomx, randomy, randomz);
			randomy = terrain.SampleHeight(currPos);

			}while(currAngle > maxSteepnes && randomy > maxYValue);
			if(randomy > maxYValue)
				Debug.Log(randomy.ToString()); //<--- this one gets called?????

At the bottom the code breaks out of the while loop even though it shouldn’t, it prints the debug message in the if statement, which means it shouldn’t of broken through that while-loop (the randomy value is bigger then maxYValue). Any thoughts?

This line:

while(currAngle > maxSteepnes && randomy > maxYValue);

Says to continue looping while both currAngle > maxSteepness and randomy > maxYValue.

If either of those conditions are false then the loop will exit.

Change your debug message to this:

if (randomy > maxYValue || currAngle > maxSteepnes)
 Debug.Log("randomY=" + randomY + ", maxYValue=" + maxYValue + ", currAngle=" + currAngle + ", maxSteepnes=" + maxSteepnes);

Either randomy will be larger than maxYValue or currAngle will be greater than maxSteepnes. Not necessarily both.