miha281
miha281

Reputation: 31

Button Color show the same time

I make a quiz game and I want my player after pressing one button of the 3 buttons to show the same time The Color of them. My script shows them without tapping them , what I should change? ( I want them without tapping to show the normal colour that is the same for them all and after the taping one of them to show for them all their colour like right and wrong answer)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RightAnswer : MonoBehaviour
{
    public Button btnR;
    public Button btnF1;
    public Button btnF2;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        btnR.GetComponent<Image>().color = Color.green;

        btnF1.GetComponent<Image>().color = Color.red;
        btnF2.GetComponent<Image>().color = Color.red;

    }
}

Upvotes: 0

Views: 38

Answers (1)

derHugo
derHugo

Reputation: 90639

Well Update is called every frame ...


The coloring during a press or hover etc is actually already handled by the Button component itself simply configure the Highlighted and Pressed Color in the Inspector.


If you mean you want the buttons to stay at the color then wait until the button is clicked you can add callbacks to Button.onClick via script like

public class RightAnswer : MonoBehaviour
{
    public Button btnR;
    public Button btnF1;
    public Button btnF2;

    private Image btnRImage;
    private Image btnF1Image;
    private Image btnF2Image;

    // Start is called before the first frame update
    private void Start()
    {
        btnRImage = btnR.GetComponent<Image>();
        btnF1Image = btnF1.GetComponent<Image>();
        btnF2Image = btnF2.GetComponent<Image>();

        // register callbacks to the buttons
        btnR.onClick.AddListener(() =>
        {
            btnRImage.color = Color.green;
        }
        );

        btnF1.onClick.AddListener(() =>
        {
            btnF1Image.color = Color.red;
        }
        );

        btnF2.onClick.AddListener(() =>
        {
            btnF2Image.color = Color.red;
        }
        );
    }
}

enter image description here


or if you ment you want to color all of them at once no matter which button was pressed (wasn't sure what you mean) then I would rather use a method like

public class RightAnswer : MonoBehaviour
{
    public Button btnR;
    public Button btnF1;
    public Button btnF2;

    private Image btnRImage;
    private Image btnF1Image;
    private Image btnF2Image;

    // Start is called before the first frame update
    private void Start()
    {
        btnRImage = btnR.GetComponent<Image>();
        btnF1Image = btnF1.GetComponent<Image>();
        btnF2Image = btnF2.GetComponent<Image>();

        // register callbacks to the buttons
        btnR.onClick.AddListener(TintAllButtons);
        btnF1.onClick.AddListener(TintAllButtons);
        btnF2.onClick.AddListener(TintAllButtons);
    }

    private void TintAllButtons()
    {
        btnRImage.color = Color.green;
        btnF1Image.color = Color.red;
        btnF2Image.color = Color.red;
    }
}

enter image description here

Upvotes: 1

Related Questions