Help with for loop

I’m trying to do a nested for loop, but according to the ouput in the log, I’m never reaching the code.

The Debug.Log(“for”) is never being reached. I’m at a loss why.

There’s no errors, just not reaching the code. :frowning:

protected virtual void attack()
	{
		for(int i=0; i<targets.Count; i++)
		{
			targets[i].dist = Vector3.Distance(transform.position, targets[i].transform.position);
			Debug.Log("before for");
			for(int cl = 0; cl< closestTargs.Count; cl++)
			{
				Debug.Log("for");
				if(closestTargs.Count <= 0)
				{
					closestTargs.Add(targets[i]);
				}
				
				Debug.Log("end of first if");
				if(targets[i].dist < closestTargs[cl].dist)
				{
					target = targets[i].gameObject;
				}
				Debug.Log("end of second if");
			}
			Debug.Log("out of for");
			if(targets[i].dist > range)
			{
				targets.RemoveAt(i);
			}
		}
	}

Because there’s nothing in your clostestTarget list to iterate over so the condition of the for loop is being met instantly.

Does closestTargs.Count == 0?

If so, I’m pretty sure that would cause that for loop to skip.

Where are you assigning “closestTargs” ? In your Debug.Log prior to your for loop, add the value of closestTargs.Count, my guess is that it’s not getting assigned properly so it’s likely empty.