Home > Software engineering >  error CS1525: Invalid expression term '-='
error CS1525: Invalid expression term '-='

Time:06-14

Im trying to add a simple attack function to my game, I use Time.DeltaTime to add a cooldown, here is the code:

        float attackCD = 0.2;
        if (attackCD < 0)
        {
            if (animation == 1)
            {
                Player.Play("SwordSwing", 0, 0.0f);
                int animation = 2;
            }
            if (animation == 2)
            {
                Player.Play("SwordSwing2", 0, 0.0f);
                int animation = 1;
            }
        }
        float attackCD -= time.DeltaTime;

This error for some reason results in error CS1525: Invalid expression term '-=' I checked the documentations on multiple websites and "-=" seems to be an existing thing, what am I doing wrong?

CodePudding user response:

You've already declared attackCD here:

float attackCD = 0.2;

but this code tries to declare it again:

float attackCD -= time.DeltaTime;

By specifying the type at the beginning you are indicating that you are declaring a new variable of that type. You already know how to use an existing variable because you're doing it elsewhere. Do the same here:

attackCD -= time.DeltaTime;

Without the attempted declaration, you can successfully decrement the variable.

  • Related