How can I make two if work togheter?

I have been searching for many methods on how to do my camera distance gets lower, 6 to 4, 4 to 2 and if the distance gets 2 and I press the button again, it gets 6.

But it does’nt work

when I do this

if (distance == 6)
{

       if (Input.GetKeyDown("v"))
      {
            distance = 4;

      }
}

the distance gets to 4
then here’s the problem
if I do this

        if (distance == 6)
        {

            if (Input.GetKeyDown("v"))
            {
                distance = 4;

            }
        }
        if (distance == 4)
        {

            if (Input.GetKeyDown("v"))
            {
                distance = 2;
            }
        }
        if (distance == 2)
        {

            if (Input.GetKeyDown("v"))
            {
                distance = 6;
            }
        }

it is always going to get 6 because when I press v, it gets to 4 then 2 then 6 again

how can I solve this?

You just need to use else if for the other ifs instead of just if. In an else if chain only one if body will be executed. If the first condition is not true, the second will be checked. If the second is not true the third will be checked, and so on. However when one condition is true the if body is executed and nothing else. The else block of an if is only executed when the condition is false.

if (distance == 6)
{
    // ...
}
else if (distance == 4)
{
    // ...
}
else if (distance == 2)
{
    // ...
}

However in your case you may want to use this instead:

if (Input.GetKeyDown("v"))
{
    distance -=2 ;
    if (distance < 2)
        distance = 6;
}

This of course assumes that “distance” can only be 2, 4 and 6. So each time you press the “v” key distance is reduced by 2. If it gets smaller than 2 we set it back to 6.

If you prefer the explicit logic over the mathematical you still should pull the keydown check out side:

if (Input.GetKeyDown("v"))
{
    if (distance == 6)
        distance = 4;
    else if (distance == 4)
        distance = 2;
    else if (distance == 2)
        distance = 6;
}