It is always executed only once for a cycle.

A short image of what my code does: enter image description here

And this is my code:

private void CheckObjectAttackPoints(Point AttackPoint){
    Point ObjectAttackPoint = AttackPoint;
    ObjectAttackPoint.X -= 1;
    int count=0; //This variable for reading how many tiles are false
    //Check tiles active and ObjectAttackPoint is exist in List
    for (int i=0; i < 1;i++) {
        if (GameManager.AllPoints.Contains (ObjectAttackPoint)) {
            if (!GameManager.TileColliders [ObjectAttackPoint.X, ObjectAttackPoint.Y].activeSelf) {
                count++;
                ObjectAttackPoints [i] = ObjectAttackPoint;
                Debug.Log (ObjectAttackPoints [i].X +", " + ObjectAttackPoints [i].Y);
            }
        }
        if (i == 1) {
            break;
        }
        ObjectAttackPoint.X += 2;
    }

    if (count > 0) {
        Debug.Log ("Object can attack " + count + " points");
    }

}

So, for this method it is required AttackPoint, which already has a value AttackPoint.Y-1, we only need to check if exists AttackPoint.Xin Listand check if the object is active at this point. When the method starts AttackPoint.X, its value decreases by 1.

My problem is that the console returns only one point, even if two tiles are inactive (Image example: 0.0 and 0.2 tiles are inactive, the console returns only count = 1 and a tile with a point of 0.0) this means that the method checks only one plate, and my code has an error, but I can not understand where it is. Can anybody help me?

+4
source share
3

for ONCE, :

private void CheckObjectAttackPoints(Point AttackPoint) {
    Point ObjectAttackPoint = AttackPoint;
    ObjectAttackPoint.X -= 1;
    int count = 0; //This variable for reading how many tiles are false
                   //Check tiles active and ObjectAttackPoint is exist in List
    if (GameManager.AllPoints.Contains(ObjectAttackPoint)) {
        if (!GameManager.TileColliders[ObjectAttackPoint.X, ObjectAttackPoint.Y].activeSelf) {
            count++;
            ObjectAttackPoints[0] = ObjectAttackPoint;
            Debug.Log(ObjectAttackPoints[0].X + ", " + ObjectAttackPoints[0].Y);
        }
    }
    ObjectAttackPoint.X += 2;

    if (count > 0) {
        Debug.Log("Object can attack " + count + " points");
    }
}

ObjectAttackPoint,

a List:

for (int i=0; i < ObjectAttackPoint.Count;i++)

array:

for (int i=0; i < ObjectAttackPoint.Length;i++)

EDIT: break i==1 ? for, if (.. i == 0 i == 1). , ObjectAttackPoints.

+4

StackOverflow. :

  • i < 1 for .
  • " ", break , .

  • for ,

  • imho .

- :

private bool CheckObjectAttackPoint(Point AttackTo)
{
   return GameManager.AllPoints.Contains(AttackTo) && !GameManager.TileColliders[AttackTo.X, AttackTo.Y].activeSelf
}

private bool CheckObjectAttackPoint(Point AttackFrom, int xDiff, yDiff)
{
   var pointToAttack = new Point(AttackFrom.X + xDiff, AttackFrom.Y + yDiff);
   return CheckObjectAttackPoint(pointToAttack);
} 

, :

Point objCurrentPoint = ...; // Currect position is (1;1)
CheckObjectAttackPoint(objCurrentPoint, -1, -1);
CheckObjectAttackPoint(objCurrentPoint, +1, -1);
+4

Error in loop definition:

for (int i = 0; i <1; i ++)

This means that the code will be executed only once, for i = 0. You can fix it as follows:

for (int i=0;i<=1;i++)
+2
source

Source: https://habr.com/ru/post/1694901/


All Articles