Why isn't my timer counting down?

I am having the most basic, entry level issue and I cannot explain why.

I have a counter that needs to count down, and for some reason it is not counting down. I’m perplexed because the logic is there, and not only that I’ve used this same script on other objects and it works fine. So I’m posting this because I must just be overlooking something simple.

Code:

            if (actionCounter > 0)
            {
                Debug.Log("actionCounter is counting down");

                actionCounter -= Time.deltaTime;

                //Do stuff
            }
            else
            {
                //Do a thing

                actionCounter = actions[currentAction].actionCounterLength;
            }

For reference, the “actionCounterLength” is 2f in the inspector, so it is getting set at 2. But if I debug it, it remains at 2 no matter what, and the countdown never starts.

Last time I checked, 2 is more than 0.

Any ideas here? It’s so simple, I feel crazy it’s not working. Thanks.

you sure this code is trying to run? do you see the actionCounter is counting down?

No exactly, I don’t see the counter counting down. It never actually runs.

However everything around it runs, and if I put the debug just before the if() it shows that “actionCounter = 2”

Because this is all happening in Update(), I see that same debug every frame. It never actually tries to count down.

so add more debug statements, so you’re saying actioncounter is 2, is it an int? is it a float?
does it do the else all the time?
what path through your code is it taking - add messages or debug it - choice is yours, but i find debug statements work well enough
You say its not running… did you even add the code to an enabled object, how is this code supposed to be triggered?

Right yeah, I’ve debugged the crap out of this thing. My only other idea is that actionCounter is being set somewhere else every frame, but tbh there is nowhere that it makes sense for that to happen. Just in case, I debugged those functions, and actionCounter always remains at 2 (or whatever is in the inspector) no matter what I do.

Just to provide some more context, I’m copying the full Update() code here to see if that helps at all explain. I’ll also include any other mentions of actionCounter.

void Update()
{

        if (!bPhaseIsOn)
        {

            if (randomizeCounter > 0)
            {
                isCountingDown = true;
                randomizeCounter -= Time.deltaTime;
            }
            else
            {
                isCountingDown = false;
                RandomizeMovement();
            }


            if (!shouldRun)
            {

                if (!isCountingDown)
                {
                    canSetVelocity = true;
                    randomizeCounter = randomizeMovementCounter;
                }

                wanderDirection = new Vector3(moveRangeX, moveRangeY, 0);
                rotateDirection = theRB.velocity;
                moveDirection = wanderDirection;
                moveDirection.Normalize();

                if (canMove)
                {
                    moveSpeed = moveSpeedWander;
                }

            }
            else
            {
                rotateDirection = transform.position - PlayerController.instance.transform.position;
                moveDirection = transform.position - PlayerController.instance.transform.position;
                moveDirection.Normalize();
                canSetVelocity = true;
            }


            float angle = Mathf.Atan2(rotateDirection.y, rotateDirection.x) * Mathf.Rad2Deg;
            Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);

            SetVelocity();



            if (theRB.velocity.x < 0 && theRB.velocity.y < 0)
            {
                transform.localScale = new Vector2(1, -1);
            }

            if (theRB.velocity.x > 0 && theRB.velocity.y < 0)
            {
                transform.localScale = new Vector2(1, 1);
            }

            if (theRB.velocity.x < 0 && theRB.velocity.y > 0)
            {
                transform.localScale = new Vector2(1, -1);
            }

            if (theRB.velocity.x > 0 && theRB.velocity.y > 0)
            {
                transform.localScale = new Vector2(1, 1);
            }
        }
    else
    {
        if (bPhaseBossFightActive)
        {

            //print("theRB.velocity.x = " + theRB.velocity.x);


            if (theRB.velocity.x > -0.1f)
            {
                return;
            }
            else
            {
                transform.localScale = new Vector3(-1f, 1f, 1f);
            }

            print("actionCounter = " + actionCounter);

            if (actionCounter > 0)
            {
                Debug.Log("actionCounter is counting down");

                actionCounter -= Time.deltaTime;

                //Movement
                moveDirection = Vector2.zero;

                if (actions[currentAction].shouldMove)
                {
                    if (actions[currentAction].shouldChasePlayer)
                    {
                        moveDirection = PlayerController.instance.transform.position - transform.position;
                        moveDirection.Normalize();
                    }

                    if (actions[currentAction].shouldMoveToPoints && Vector3.Distance(transform.position, actions[currentAction].pointToMoveTo.position) > .5f)
                    {
                        moveDirection = actions[currentAction].pointToMoveTo.position - transform.position;
                        moveDirection.Normalize();
                    }
                }


                theRB.velocity = moveDirection * actions[currentAction].moveSpeed;

                //Shooting
                if (actions[currentAction].shouldShoot)
                {
                    shotCounter -= Time.deltaTime;
                    if (shotCounter <= 0)
                    {
                        shotCounter = actions[currentAction].timeBetweenShots;

                        if (//DataManager.instance.difficulty > 0
                            PlayerPrefs.GetInt("difficulty") > 0)
                        {

                            foreach (Transform t in actions[currentAction].shotPoints)
                            {
                                Instantiate(actions[currentAction].itemToShoot, t.position, t.rotation);
                                AudioManager.instance.PlaySFX(enemyShootSound);
                            }
                        }
                    }
                }
            }
            else
            {
                currentAction++;

                if (currentAction >= actions.Length)
                {
                    currentAction = 0;
                }

                //print("actionCounter  = " + actionCounter);
                //print("actions.Length  = " + actions.Length);
                //print("actions[currentAction]  = " + actions[currentAction]);
                //print("actions[currentAction].actionLength  = " + actions[currentAction].actionCounterLength);

                actionCounter = actions[currentAction].actionCounterLength;

            }
        }
    }


    if (!canMove)
    {
        moveSpeed = 0;
        ResetHeadPosition();
    }
    else
    {
        SetVelocity();
    }
}

Then this is where I push the enemy to a new sequence, and reset the actionCounter (of course the next sequence never begins, because the actionCounter never counts down in the first place):

private void NextSequence()
{
    PlayWakeUpSound();
    currentSequence++;
    actions = sequences[currentSequence].sequenceActions;
    currentAction = 0;
    actionCounter = actions[currentAction].actionCounterLength;
    anim.SetBool("isWalking", false);
}

This is where the initial actionCounter is set for the first time. Considering it’s an IEnumerator called only once, it won’t ever be running more than once:

public IEnumerator CreateHead()
{
    //print("should create head");

    anim.SetTrigger("createHead");
    anim.SetBool("isIdle", false);
    yield return new WaitForSeconds(1f);

    bPhaseBossFightActive = true;
    //UIController.instance.bossHealthBar.gameObject.SetActive(true);

    maxHealth = 1200;

    if (PlayerPrefs.GetInt("difficulty") >= 2)
    {
        newHealth = Mathf.FloorToInt(maxHealth * 2f);
        maxHealth = newHealth;

        
    }

    currentHealthB = maxHealth;

    actions = sequences[currentSequence].sequenceActions;
    actionCounter = actions[currentAction].actionCounterLength;

    UIController.instance.bossHealthBar.maxValue = maxHealth;
    UIController.instance.bossHealthBar.value = currentHealthB;


    //CheckAllAnimationStates();



}

FYI I resolved this. Very simple bug. I was “returning” in this snippet, and Update() was being exited.

I hate when things are so simple and they just stare you in the face sometimes like this, but you can’t figure it out. Good lord help me.

Sorry i never saw you had replied to look :frowning:
glad you found it