Daniel Lip
Daniel Lip

Reputation: 11319

How can I toggle between two flags boolean?

public bool animCurve = true;
public bool straightLine = false;

Then in the Update:

void Update()
    {
        if (objectsToMove != null && objectsToMove.Length > 0)
        {
            if (animCurve)
            {
                straightLine = false;
                AnimationCurve();
            }

            if (straightLine)
            {
                animCurve = false;
                StraightLineTrack();
            }
        }
    }

When running the game the animCurve is true but if I click on the straightLine nothing happen. I need first to uncheck the animCurve then to check the straightLine.

But if the straightLine is not checked true I can check the animCurve to be true and it will automatic change the straightLine to false. Why it's not working the other way ? Why I need first to uncheck the animCurve and then check true the straightLine ?

I want that when I check set true one of the flags the other one will automatic set to false.

Upvotes: 2

Views: 189

Answers (1)

Programmer
Programmer

Reputation: 125275

You have two boolean variables but you only want one to be true. When one is true, the other is false. This is where enum should be used to simplify your logic.

Your enum:

public enum LineType
{
    Curve, Straight
}

Declare and check which one is enabled:

LineType lineType = LineType.Straight;

void Update()
{
    if (lineType == LineType.Straight)
    {
        StraightLineTrack();
    }

    else if (lineType == LineType.Curve)
    {
        AnimationCurve();
    }
}

To change it to curve:

lineType = LineType.Curve;

Upvotes: 2

Related Questions