After I added Timer.EndTiming() the buttons aren't function when the first click

Why the buttons are out of function when the first click, however they reaction normally when the second click.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using UnityEngine.UI;

public class Quiz : MonoBehaviour

{

[Header("Quiz")]

[SerializeField] TextMeshProUGUI QuestionText;

[SerializeField] QuizDataController QuizData;

[Header("Button")]

[SerializeField] Sprite DefaultButtonImage;

[SerializeField] Sprite CorrectButtomImage;

[SerializeField] GameObject[] AnswerButton;

[Header("Timer")]

Timer Timer;

[SerializeField] Image TimerImage;

void Start()

{

    DisplayQuestion();

    //LoadNewQuestion();

    Timer = FindObjectOfType<Timer>(); //引用 scrpit Timer

}

void Update()

{

    TimerImage.fillAmount = Timer.FillFraction;

    if(Timer.LoadNextQuestion)

    {

        LoadNewQuestion();

        Timer.LoadNextQuestion = false;

    }

}

void DisplayQuestion()

{

    QuestionText.text = QuizData.GetQuizText();

    ChangeAnswerButtonText(AnswerButton.Length);

}

void ChangeAnswerButtonText(int index)

{

    for(int i=0; i<index ; i++)

    {

        AnswerButton[i].GetComponentInChildren<TextMeshProUGUI>().text=QuizData.GetQuizAnwser(i);

    }

}

public void OnAnswerSelected( int index)

{

    DiplayAnswer(index);

    ButtonState(false);

    Timer.EndTiming();//After I added this script the bug begin.

}

void DiplayAnswer(int index)

{

    Image ButtonImage;

    int GetCorrectAnswerIndex = QuizData.GetCorrectAnswerIndex();

       

    if(index == GetCorrectAnswerIndex)

    {

        QuestionText.text = "Correct!!";

        ButtonImage = AnswerButton[index].GetComponent<Image>();

        ButtonImage.sprite = CorrectButtomImage;

    }

    else

    {

        QuestionText.text = $"Wrong!! The anwser is \n { AnswerButton[GetCorrectAnswerIndex].GetComponentInChildren<TextMeshProUGUI>().text} ";

        ButtonImage = AnswerButton[GetCorrectAnswerIndex].GetComponent<Image>();

        ButtonImage.sprite = CorrectButtomImage;

    }

}

void LoadNewQuestion()

{

 ButtonState(true);

 SetDefaultButtonSprites();

 DisplayQuestion();

}

void ButtonState(bool State)

{

    for(int i=0; i<AnswerButton.Length;i++)

    {

        AnswerButton[i].GetComponent<Button>().interactable = State;

    }

}

void SetDefaultButtonSprites()

{

    for (int i = 0 ; i<AnswerButton.Length; i++)

    {

        AnswerButton[i].GetComponent<Image>().sprite = DefaultButtonImage;

    }

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Timer : MonoBehaviour

{

[SerializeField] float AnsweringTime = 15f;

[SerializeField] float ShowAnswerTime = 5f;



float Timing;

public bool IsAnserQuestion = false;

public float FillFraction;

public bool LoadNextQuestion;

void Update()

{

    UpdateTimer();

}

void UpdateTimer()

{

    Timing -= Time.deltaTime; //  Timing = Timing - Time.deltaTime

    if(Timing <= 0 && !IsAnserQuestion )

    {

        Timing = AnsweringTime;

        IsAnserQuestion = true;

    }

    else if (Timing <=0 && IsAnserQuestion)

    {

        Timing = ShowAnswerTime;

        IsAnserQuestion = false;

        LoadNextQuestion = true;

    }

    else if(Timing > 0 && IsAnserQuestion)

    {

        FillFraction = Timing/AnsweringTime;

    }

    else if(Timing > 0 && !IsAnserQuestion)

    {

        FillFraction = Timing /ShowAnswerTime;

    }

    //Debug.Log(IsAnserQuestion+":"+FillFraction+"/"+Timing);

}

public void EndTiming()

{

    Timing = 0;

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

[CreateAssetMenu(menuName = “Create New Quiz”, fileName = “New Quiz”)]

public class QuizDataController : ScriptableObject

{

[TextArea(2,6)][SerializeField]string Quiz = "Type your Quiz";

[SerializeField] string [] QuizAnswer = new string[4] ;

[SerializeField] int CorrectAnswerIndex;

public string GetQuizText()

{

    return Quiz;

}

public int GetCorrectAnswerIndex()

{

    return CorrectAnswerIndex;

}

public string GetQuizAnwser(int Index)

{

    return QuizAnswer[Index];

}

}

The flim for reference

Hi @NI_You,

Welcome to our community! :slight_smile:

Is there an EventSystem in your Hierarchy? If not, add one.


See also:

I found out the issue was because I load new question in the wrong timing.
I revised it as following:

void UpdateTimer()

{

    TimingValue -= Time.deltaTime; //  Timing = Timing - Time.deltaTime

    if(TimingValue <= 0 && !HasAnswerQuestion)

    {

        LoadNextQuestion = true;

        TimingValue = AnsweringTime;

        HasAnswerQuestion = true;

    }

    else if (TimingValue <=0 && HasAnswerQuestion)

    {

        TimingValue = ShowAnswerTime;

        HasAnswerQuestion = false;

        //LoadNextQuestion = true; // wrong LoadNextQuestion Timing

    }

    else if(TimingValue > 0 && HasAnswerQuestion)

    {

        FillFraction = TimingValue/AnsweringTime;

    }

    else if(TimingValue > 0 && !HasAnswerQuestion)

    {

        FillFraction = TimingValue /ShowAnswerTime;

    }

    //Debug.Log(IsAnserQuestion+":"+FillFraction+"/"+Timing);

}

Good job on solving the problem! :slight_smile:

1 Like

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

Privacy & Terms