Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error CS1525: Invalid expression term '-='

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading