InvokeRepeating time doesn't sync within the same update function

As the title suggests, I basically have two identical InvokeRepeating loops in my script, and one works as it should while the other doesn’t.

    void Update () {


        if (Input.GetKeyDown(KeyCode.Z) & (grass1 == false)) {

            beginning = false;

            grassone = true;
            grasstwo = false;
            grassthree = false;

            grass1 = true;
            grass2 = false;
            grass3 = false;

            InvokeRepeating("Grass1", 0, .25f);
        }

        if (Input.GetKeyDown(KeyCode.X) & (grass2 == false)) {

            beginning = false;

            grassone = false;
            grasstwo = true;
            grassthree = false;

            grass1 = false;
            grass2 = true;
            grass3 = false;

            InvokeRepeating("Grass2", 0, .25f);
        }

        if (Input.GetKeyDown(KeyCode.C) & (grass3 == false)) {

            beginning = false;

            grassone = false;
            grasstwo = false;
            grassthree = true;

            grass1 = false;
            grass2 = false;
            grass3 = true;
           
            InvokeRepeating("Grass3", 0, .25f);
        }

        if (grassone == true) {
            CancelInvoke("Grass2");
            CancelInvoke("Grass3");
        }
        if (grasstwo == true) {
            CancelInvoke("Grass1");
            CancelInvoke("Grass3");
        }
        if (grassthree == true) {
            CancelInvoke("Grass1");
            CancelInvoke("Grass2");
        }


        if (beginning == true) {
            InvokeRepeating("Grass2", 0, .25f);
        }

    }

There’s my entire update function, I’m sure most of it is unnecessary but I didn’t want to leave anything important out. My issue is this part:

if (beginning == true) {
            InvokeRepeating("Grass2", 0, .25f);
        }

Instead of invoking the Grass2 function every .25 seconds like

 if (Input.GetKeyDown(KeyCode.X) & (grass2 == false)) {

            beginning = false;

            grassone = false;
            grasstwo = true;
            grassthree = false;

            grass1 = false;
            grass2 = true;
            grass3 = false;

            InvokeRepeating("Grass2", 0, .25f);
        }

does perfectly, it does it at a much, much faster rate. It is worth noting that I installed Unity and Visual Studios a week ago and am very new, so I’m probably missing something obvious. Any help would be appreciated, thanks!

I think the problem is that you do not set beginning = false until any triggering key is hit. Therefore you Execute the lines

if (beginning == true) {
     InvokeRepeating("Grass2", 0, .25f);
}

each frame until any of the keys is hit, therefore starting many “instances of repeating invokations”, which accumulate and end up calling the “Grass2” method at a much faster rate.
You should remove that block of code and just change the condition for “Grass2” to be invoked repeatedly in the other block of code:

if ((Input.GetKeyDown(KeyCode.X) & (grass2 == false)) || beginning) {
    ...
    InvokeRepeating("Grass2", 0, .25f);
}

This way, it is also ensured that the invocations don’t accumulate when you try to start “Grass2” with the key press after it has already be called on “beginning”.

i guess it’s because you’re calling invokerepeating endlessly… the void Update() part will always call your invokerepeating part so what i mean, in all other methods you’re calling invokerepeating, it’s an if statement, which turns false imediatly because your reference getkeydown.

that’s not happening on your last invoke.


what you might wanna do is setting beginning right after the invokepart to false, or create a new boolean just for the beginning part.


i hope i was clear enough :slight_smile: feel free to ask