Infinite Loop While instantiating tiles to form a Scattered Line

Hey all, the problem is probably simple, but I keep getting an infinite loop when I go through the following:

public static void GenerateLeftPoints(int NumberOfJitters, string Path, List<Vector2> AllVectorJitters)//, Vector2 StartPointP, Vector2 EndPointP
        {
            //Vectors
            Vector2 StartPoint = new Vector2(1, 1);
            Vector2 EndPoint = new Vector2(1, 1);
            Vector2 CurrentPoint = new Vector2(1, 1);
            Vector2 LastEndPoint = new Vector2(1, 1);
            Vector2 GetLastEndPoint = new Vector2(1, 1);


            //Assign the vectors in a for loop
            for (int i = 0; i < NumberOfJitters; i++)
            {
                Vector2 StartPointP = new Vector2(UnityEngine.Random.Range(0, ContinentGen.MapSizeX), UnityEngine.Random.Range(0, ContinentGen.MapSizeY));
                Vector2 EndPointP = new Vector2(UnityEngine.Random.Range(0, ContinentGen.MapSizeX), UnityEngine.Random.Range(0, StartPointP.y));

                Debug.Log("StartPointP: " + i + " " + StartPointP);
                Debug.Log("EndPointP: " + i + " " + EndPointP);
                Debug.Log("I: " + i);

                bool Up = false;
                bool Down = false;
                bool Right = false;
                bool Left = false;

                if (i == 0)
                {
                    Debug.Log("i == 0");
                    StartPoint = StartPointP;
                    CurrentPoint = StartPoint;
                    EndPoint = new Vector2(UnityEngine.Random.Range(0, StartPointP.x), UnityEngine.Random.Range(0, StartPointP.y));
                }
                if (i > 0 && i != NumberOfJitters - 1)
                {
                    Debug.Log("i is just greater than 0");
                    LastEndPoint = GetLastEndPoint;
                    StartPoint = LastEndPoint;
                    CurrentPoint = StartPoint;
                    EndPoint = new Vector2(UnityEngine.Random.Range(0, StartPointP.x), UnityEngine.Random.Range(0, StartPoint.y));
                }
                if (i > 0 && i == NumberOfJitters - 1)
                {
                    Debug.Log("This should be happening once");
                    LastEndPoint = GetLastEndPoint;
                    StartPoint = LastEndPoint;
                    CurrentPoint = StartPoint;
                    EndPoint = EndPointP;
                }
                else
                {

                    LastEndPoint = new Vector2(1,1);
                    StartPoint = new Vector2(1,1);
                    CurrentPoint = new Vector2(1,1);
                    EndPoint = new Vector2(1,1);
                }

                ////Negative = Positive and vice versa

                float TotalDistanceX = StartPoint.x - EndPoint.x;
                float TotalDistanceY = StartPoint.y - EndPoint.y;

                Debug.Log("i Value: " + i + " TotalDistanceX: " + TotalDistanceX + "TotalDistanceY: " + TotalDistanceY);
                //Directional Checks
                //Moving Right if this is true
                if (TotalDistanceX < 0)
                {
                    Right = true;
                }
                //Moving Left if this is true
                if (TotalDistanceX > 0)
                {
                    Left = true;
                }
                //Moving Up if this is true
                if (TotalDistanceY < 0)
                {
                    Up = true;
                }
                //Moving down if this is true
                if (TotalDistanceY > 0)
                {
                    Down = true;
                }

                if (Right == true)
                {
                    Debug.Log("Right is true");
                    while (TotalDistanceX != 0)
                    {
                        CurrentPoint.x += 1;
                        Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                        TotalDistanceX++;
                        if (Up == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y += 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY++;

                        }
                        if (Down == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y -= 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY--;
                        }
                    }


                }

                if (Left == true)
                {
                    while (TotalDistanceX != 0)
                    {
                        CurrentPoint.x -= 1;
                        Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                        TotalDistanceX--;
                        if (Up == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y += 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY++;

                        }
                        if (Down == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y -= 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY--;
                        }
                    }
                }
                GetLastEndPoint = CurrentPoint;
            }
        }

The only way it works is if I put in the Else statement. Now the else statement wouldn’t be a problem except it ALWAYS runs and gets rid of the previous Declarations. This means that i don’t get any randomness to my scattered line. It was adapted from the following code I made earlier:

//Vectors
            Vector2 StartPoint;
            Vector2 EndPoint;
            Vector2 CurrentPoint;
            Vector2 LastEndPoint;
            Vector2 GetLastEndPoint = new Vector2(1, 1);



            //Assign the vectors in a for loop
            for (int i = 0; i < NumOfJit; i++)
            {
                bool Up = false;
                bool Down = false;
                bool Right = false;
                bool Left = false;

                if (i == 0)
                {
                    StartPoint = new Vector2(UnityEngine.Random.Range(0, ContinentGen.MapSizeX), UnityEngine.Random.Range(0, ContinentGen.MapSizeY));
                    CurrentPoint = StartPoint;
                    EndPoint = new Vector2(UnityEngine.Random.Range(0, ContinentGen.MapSizeX), UnityEngine.Random.Range(0, ContinentGen.MapSizeY));
                }
                if (i > 0)
                {
                    LastEndPoint = GetLastEndPoint;
                    StartPoint = LastEndPoint;
                    CurrentPoint = StartPoint;
                    EndPoint = new Vector2(UnityEngine.Random.Range(0, ContinentGen.MapSizeX), UnityEngine.Random.Range(0, ContinentGen.MapSizeY));
                }
                else
                {
                    StartPoint = new Vector2(1, 1);
                    CurrentPoint = new Vector2(1, 1);
                    EndPoint = new Vector2(1, 1);
                }

                //Negative = Positive and vice versa

                float TotalDistanceX = StartPoint.x - EndPoint.x;
                float TotalDistanceY = StartPoint.y - EndPoint.y;

                //Directional Checks
                //Moving Right if this is true
                if (TotalDistanceX < 0)
                {
                    Right = true;
                }
                //Moving Left if this is true
                if (TotalDistanceX > 0)
                {
                    Left = true;
                }
                //Moving Up if this is true
                if (TotalDistanceY < 0)
                {
                    Up = true;
                }
                //Moving down if this is true
                if (TotalDistanceY > 0)
                {
                    Down = true;
                }

                if (Right == true)
                {
                    while (TotalDistanceX != 0)
                    {
                        CurrentPoint.x += 1;
                        Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                        TotalDistanceX++;
                        if (Up == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y += 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY++;

                        }
                        if (Down == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y -= 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY--;
                        }
                    }


                }

                if (Left == true)
                {
                    while (TotalDistanceX != 0)
                    {
                        CurrentPoint.x -= 1;
                        Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                        TotalDistanceX--;
                        if (Up == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y += 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY++;

                        }
                        if (Down == true && TotalDistanceY != 0)
                        {
                            CurrentPoint.y -= 1;
                            Instantiate(Resources.Load(Path), CurrentPoint, Quaternion.identity);
                            TotalDistanceY--;
                        }
                    }
                }
                GetLastEndPoint = CurrentPoint;
            }

Notice the only differences are that I only want it to Spawn new points that are below StartPointP point and above EndPointP, And that the new points that are spawned can only to be the left of StartPointP.

I’m at a bit of a loss here… And before people answer “Just use the old one that works.” I would but the end goal is to save where this chained line ends and use that as the same end point for ANOTHER chained line that will fill in the right side. I will then take all of the vectors that are instantiated and fill from left to right, a body. I plan on using this to make continents, lakes, and anything that needs to be filled randomly. The Original Was for mountain ranges And i figured i could just re use a lot of the code.

Sorry if this didnt make sense lol…

TLDR: Unless i include the else statement in the first bit of code i get an infinite loop. The problem here being it ALWAYS runs the else statement AND the other statements… It’s like it isn’t recognizing the I changes, and yet When i debug check it is…

The reason that your else statement is overwriting your data points is because the “else” is only part of the last if statement block you have there. So you have 3 ifs that always happen and an else that is always happening because the if before it fails on all data points.

Example 1 “The Issue”:

	void DoStuff(){
		for(int i = 0; i < 10;i++){
			if(i == 0){
				// Happens once at beginning
			}
			if(i > 0 && i < 5){
				// Happens for iterations 1 -> 4
			}
			
			//--if/else block starts here
			if(i != 0 && i == 9){
				// Happens only at iteration 8
			}
			else{
				// Happens whenever the previous if fails, so, all iterations except 0 and 8
			}
			//--if/else block ends here
		}
	}

Example 2 “The Solution”:

			if (i == 0){
				Debug.Log("i == 0");
				StartPoint = StartPointP;
				CurrentPoint = StartPoint;
				EndPoint = new Vector2(Random.Range(0, StartPointP.x), Random.Range(0, StartPointP.y));
			}
			
			else if (i > 0 && i != NumberOfJitters - 1){
				Debug.Log("i is just greater than 0");
				LastEndPoint = GetLastEndPoint;
				StartPoint = LastEndPoint;
				CurrentPoint = StartPoint;
				EndPoint = new Vector2(Random.Range(0, StartPointP.x), Random.Range(0, StartPoint.y));
			}
			
			else if (i > 0 && i == NumberOfJitters - 1){
				Debug.Log("This should be happening once");
				LastEndPoint = GetLastEndPoint;
				StartPoint = LastEndPoint;
				CurrentPoint = StartPoint;
				EndPoint = EndPointP;
			}
			else{
				
				LastEndPoint = new Vector2(1,1);
				StartPoint = new Vector2(1,1);
				CurrentPoint = new Vector2(1,1);
				EndPoint = new Vector2(1,1);
			}