Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question I have a problem with how to change the color of a botton. Please help is urgent. Thank you :)

Discussion in 'Scripting' started by brolonoayt, Apr 28, 2021.

  1. brolonoayt

    brolonoayt

    Joined:
    Jan 14, 2021
    Posts:
    3
    I'm doing a trivia game for a class assignment.
    When I press the correct button it changes color to green and when I press the wrong one it changes to red, the problem is that the following buttons with the new answers remain the color of the previous one. I do not know how to solve this problem if you can help me I would appreciate it a lot. Here I leave my script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class Answer1 : MonoBehaviour
    {
    public bool isCorrect = false;
    public QuizManager quizManager;
    public Color startColor;
    public void Answe()
    {
    if (isCorrect)
    {
    GetComponent<Image>().color = Color.green;
    Debug.Log("Correct Answer");
    quizManager.correct();

    }
    else
    {
    GetComponent<Image>().color = Color.red;
    Debug.Log("Wrong Answer");
    quizManager.wrong();

    }
    }
    }
     
  2. KalOBrien

    KalOBrien

    Unity Technologies

    Joined:
    Apr 20, 2021
    Posts:
    89
    I'd say have a method that resets the button color before the new trivia question appears

    Assign two variables to reference the buttons
    something like:
    Code (CSharp):
    1. public GameObject Button1;
    2. public GameObject Button2;
    3.  
    4. public void Reset(gameobject Button){
    5. Button.GetComponent<Image>().color = Color.white;
    6. }
    Then when you're about to reset the questions just call the Reset method.

    like:

    Reset(Button1);
    Reset(Button2);

    ALSO remember to assign the buttons in the inspector window in the editor.

    I hope that helps!
     
  3. brolonoayt

    brolonoayt

    Joined:
    Jan 14, 2021
    Posts:
    3
    thx I'm going to try it
     
  4. brolonoayt

    brolonoayt

    Joined:
    Jan 14, 2021
    Posts:
    3
    I have done what you have told me, but I had not thought that as the question changes immediately, the color is reset before it can be seen, is there any way that it takes a while or that it remains stopped for a few seconds before moving on to the next questions?. Thank you very much anyway
     
  5. KalOBrien

    KalOBrien

    Unity Technologies

    Joined:
    Apr 20, 2021
    Posts:
    89
    I would suggest a variable timer.

    Something that the Timer gets set to the time you want it to run, and decrease it over time, if its 0, go to the next question.


    Code (CSharp):
    1. float timer =0;
    2.  
    3. if(timer>timerlength){
    4.  
    5. timer-= time.deltaTime;
    6.  
    7. }
    8. if(timer ==0){
    9. NextQuestion();
    10. }
    Its very loose but this is the kind of logic behind it. Lemme know if it helps!
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Comparing a float to an integer with
    ==
    is a bad idea.
    Mathf.Approximately


    But if you count down to zero, then more likely
    Code (CSharp):
    1. if (timer < 0f)
    2. {
    3.     NextQuestion();
    4.     timer += timerlength;
    5. }
     
    KalOBrien likes this.
  7. KalOBrien

    KalOBrien

    Unity Technologies

    Joined:
    Apr 20, 2021
    Posts:
    89
    Oh thats actually very fair. It's been awhile since I've done a timer but with it being a float if its even a decimal off it wont ever trigger. "<=" could work here as well. Thanks for pointing out the error!
     
    Lurking-Ninja likes this.
  8. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    108
    You can also make use of Coroutines:

    Code (CSharp):
    1.  
    2. IEnumerator void NextQuestionCO()
    3. {
    4.     //you can do stuff immediately
    5.  
    6.     yield return new WaitForSeconds(5f); //before continuing execution, the script is waiting 5sec
    7.  
    8.     //Do other stuff after 5 seconds
    9.  
    10.     ///yield again if you need and do other stuff
    11. }
    12.  
    than you can call the coroutine when you answer the question like:

    Code (CSharp):
    1. public void Answer()
    2. {
    3.    if(isCorrect) ///dostuff
    4.    else //doOtherstuff
    5.  
    6.    StartCoroutine(NextQuestionCO());
    7. }
     
    KalOBrien likes this.
  9. KalOBrien

    KalOBrien

    Unity Technologies

    Joined:
    Apr 20, 2021
    Posts:
    89
    This is the cleanest way I think I've ever seen someone explain Coroutines.
     
    M4dR0b likes this.