Fill Fraction

The fill value for Answer Question starts above 1, leading to a delay of the timer image animation.

That will only happen if the timerValue is greater than timeToCompleteQuestion or timeToShowCorrectAnswer (depending on which state your timer is in). When the state is isAnsweringQuestion it would have been set to timeToCompleteQuestion in the previous state which means it can only be less. If the state is not isAnsweringQuestion it would heve been set to timeToShowCorrectAnswer which also means it can only be less. So there must be a problem in your code. Can you share the code so we can check?

Hey, I spent several hours debugging it, but it seems to be working now.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Timer : MonoBehaviour

{

[SerializeField] private float timeToCompleteQuestion = 30f;

[SerializeField] private float timeToShowCorrectAnswer = 10f;

public bool isAnswerQuestion = false;

public bool loadNextQuestion;

[SerializeField] private float fillFraction;

private float timerValue;

private float initialTimerValue;

public float FillFraction

{

    get { return fillFraction; }

    private set { fillFraction = value; }

}

void Start()

{

    InitializeTimer();

}

void Update()

{

    UpdateTimer();

}

public void CancelTimer()

{

    timerValue = 0;

}

void InitializeTimer()

{

    initialTimerValue = isAnswerQuestion ? timeToShowCorrectAnswer : timeToCompleteQuestion;

    timerValue = initialTimerValue;

}

void UpdateTimer()

{

    timerValue -= Time.deltaTime;

    if (timerValue <= 0)

    {

        if (isAnswerQuestion)

        {

            isAnswerQuestion = false;

            timerValue = timeToShowCorrectAnswer;

        }

        else

        {

            isAnswerQuestion = true;

            timerValue = timeToCompleteQuestion;

            loadNextQuestion = false;

        }

    }

    else

    {

        FillFraction = timerValue / initialTimerValue;

    }

    Debug.Log(isAnswerQuestion + ": " + timerValue + " = " + FillFraction);

}

}
I kept the timerValue as <= 0. Looks like I had to go through a few extra steps to get it working.

What I was doing seemed way too complicated so I just followed the instructor instead.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms