Changing 2 button colors with 1 button click

Hi im just starting learning Unity and is now following a tutorial, in the tutorial when user clicked it changes the button color to green when its the right answer and red when its wrong answer, i was wondering if i could make it so when i pressed the wrong button it would still become red on the button i clicked but the right button turn green when i clicked the wrong button

edit : to include the entire code so its easier to understand after commenter suggestion, its my fist time using this forum

this is the QuizManager code:


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

public class QuizManager : MonoBehaviour
{
    /* SerializeField digunakan agar bisa mengedit object di Unity Editor */
    [SerializeField] private QuizUI quizUI;
    [SerializeField] 
    private List<Pertanyaan> pertanyaan;
    private Pertanyaan PertanDipilih;

    void Start()
    {
        PilihPertan();
    }

    void PilihPertan()
    {
        int val = Random.Range(0, pertanyaan.Count);
        PertanDipilih = pertanyaan[val];

        quizUI.AturPertan(PertanDipilih);
    }

  
    public bool Jawaban(string dijawab)
    {
        bool jawabBnr = false;
        if ( dijawab == PertanDipilih.jawabanBnr)
        {
            jawabBnr = true; 
        }
        else
        {
            //No
        }

        Invoke("PilihPertan", 1.5f);
        return jawabBnr;
    }
}

[System.Serializable]
public class Pertanyaan
{
    public string InfoPertan;
    public TipePertan tipePertan;
    public Sprite pertaImg;
    public AudioClip PertaClip;
    public UnityEngine.Video.VideoClip PertaVideo;
    public List<string> opsis;
    public string jawabanBnr;
}

[System.Serializable]

public enum TipePertan
{
    TEKS,
    GAMBAR,
    VIDEO,
    AUDIO
}

this is the QuizUI code:

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

public class QuizUI : MonoBehaviour
{
    [SerializeField] private QuizManager quizManager;
    [SerializeField] private TMP_Text Tekspertan;
    [SerializeField] private Image Gambarpertan;
    [SerializeField] private UnityEngine.Video.VideoPlayer Videopertan;
    [SerializeField] private AudioSource Audiopertan;
    [SerializeField] private List<Button> opsis;
    [SerializeField] private Color JbnrCOl, JslhCOl, normalCOl;
    

    private Pertanyaan pertanyaan;
    private bool dijawab;
    private float durasiAudio;

    void Awake()
    {
        for (int i = 0; i < opsis.Count; i++)
        {
            Button lokalBtn = opsis[i];
            lokalBtn.onClick.AddListener(() => Onclick(lokalBtn));
        }
    }

   public void AturPertan(Pertanyaan pertanyaan)
    {
        this.pertanyaan = pertanyaan;

        switch(pertanyaan.tipePertan)
        {
            case TipePertan.TEKS:
                Gambarpertan.transform.parent.gameObject.SetActive(false);
                break;

            case TipePertan.GAMBAR:
                TempatMedia();
                Gambarpertan.transform.gameObject.SetActive(true);
                Gambarpertan.sprite = pertanyaan.pertaImg;
                break;

            case TipePertan.VIDEO:
                TempatMedia();
                Videopertan.transform.gameObject.SetActive(true);
                Videopertan.clip = pertanyaan.PertaVideo;
                break;

            case TipePertan.AUDIO:
                TempatMedia();
                Audiopertan.transform.gameObject.SetActive(true);

                durasiAudio = pertanyaan.PertaClip.length;
                StartCoroutine(PlayAudio());
                break;
        }

        Tekspertan.text = pertanyaan.InfoPertan;

        List<string> Listjawaban = ShuffleList.ShuffleListItems<string>(pertanyaan.opsis);

        for (int i = 0; i < opsis.Count; i++)
        {
            opsis[i].GetComponentInChildren<TMP_Text>().text = Listjawaban[i];
            opsis[i].name = Listjawaban[i];
            opsis[i].image.color = normalCOl;
        }

        dijawab = false;
    }

    IEnumerator PlayAudio()
    {
        if(pertanyaan.tipePertan == TipePertan.AUDIO)
        {
            Audiopertan.PlayOneShot(pertanyaan.PertaClip);

            yield return new WaitForSeconds(durasiAudio + 0.5f);

            StartCoroutine(PlayAudio());
        }
        else
        {
            StopCoroutine(PlayAudio());
            yield return null;
        }
    }

    void TempatMedia()
    {
        Gambarpertan.transform.parent.gameObject.SetActive(true);
        Gambarpertan.transform.gameObject.SetActive(false);
        Audiopertan.transform.gameObject.SetActive(false);
        Videopertan.transform.gameObject.SetActive(false);
    }

    
    public void Onclick(Button btn)
    {
        
        if (!dijawab)
        {
            dijawab = true;
            bool val = quizManager.Jawaban(btn.name);

            if (val)
            {
                btn.image.color = JbnrCOl;
            }
            else
            {
                btn.image.color = JslhCOl;
               
            }

        }
    }
}

The tutorial i was following is this

Hello, tangdilintina :slight_smile:

Unfortunately, I don’t have everything set up in Unity, so I can’t run and test the code.
Let me know if you run into problems implementing this.

  • In QuizManager I added the method GetCorrectAnswer() which returns a string.

  • In QuizUI I added the method GetCorrectButton() which returns a Unity Button.

  • Don’t forget using System.Linq; at the top of the QuizUI file. It’s needed for the built-in List.Find(..) method that I used inside GetCorrectButton().

Search for the // Added by Ady comments so you can easily find all the places where I added some code.

QuizManager

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

public class QuizManager : MonoBehaviour
{
    /* SerializeField digunakan agar bisa mengedit object di Unity Editor */
    [SerializeField] private QuizUI quizUI;
    [SerializeField] 
    private List<Pertanyaan> pertanyaan;
    private Pertanyaan PertanDipilih;

    void Start()
    {
        PilihPertan();
    }

    void PilihPertan()
    {
        int val = Random.Range(0, pertanyaan.Count);
        PertanDipilih = pertanyaan[val];

        quizUI.AturPertan(PertanDipilih);
    }

  
    public bool Jawaban(string dijawab)
    {
        bool jawabBnr = false;
        if ( dijawab == PertanDipilih.jawabanBnr)
        {
            jawabBnr = true; 
        }
        else
        {
            //No
        }

        Invoke("PilihPertan", 1.5f);
        return jawabBnr;
    }
    
    // Added by Ady
    public string GetCorrectAnswer()
    {
        return PertanDipilih.jawabanBnr;
    }
}

[System.Serializable]
public class Pertanyaan
{
    public string InfoPertan;
    public TipePertan tipePertan;
    public Sprite pertaImg;
    public AudioClip PertaClip;
    public UnityEngine.Video.VideoClip PertaVideo;
    public List<string> opsis;
    public string jawabanBnr;
}

[System.Serializable]

public enum TipePertan
{
    TEKS,
    GAMBAR,
    VIDEO,
    AUDIO
}

QuizUI

using System.Collections;
using System.Collections.Generic;
using System.Linq; // Added by Ady
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class QuizUI : MonoBehaviour
{
    [SerializeField] private QuizManager quizManager;
    [SerializeField] private TMP_Text Tekspertan;
    [SerializeField] private Image Gambarpertan;
    [SerializeField] private UnityEngine.Video.VideoPlayer Videopertan;
    [SerializeField] private AudioSource Audiopertan;
    [SerializeField] private List<Button> opsis;
    [SerializeField] private Color JbnrCOl, JslhCOl, normalCOl;
    

    private Pertanyaan pertanyaan;
    private bool dijawab;
    private float durasiAudio;

    void Awake()
    {
        for (int i = 0; i < opsis.Count; i++)
        {
            Button lokalBtn = opsis[i];
            lokalBtn.onClick.AddListener(() => Onclick(lokalBtn));
        }
    }

   public void AturPertan(Pertanyaan pertanyaan)
    {
        this.pertanyaan = pertanyaan;

        switch(pertanyaan.tipePertan)
        {
            case TipePertan.TEKS:
                Gambarpertan.transform.parent.gameObject.SetActive(false);
                break;

            case TipePertan.GAMBAR:
                TempatMedia();
                Gambarpertan.transform.gameObject.SetActive(true);
                Gambarpertan.sprite = pertanyaan.pertaImg;
                break;

            case TipePertan.VIDEO:
                TempatMedia();
                Videopertan.transform.gameObject.SetActive(true);
                Videopertan.clip = pertanyaan.PertaVideo;
                break;

            case TipePertan.AUDIO:
                TempatMedia();
                Audiopertan.transform.gameObject.SetActive(true);

                durasiAudio = pertanyaan.PertaClip.length;
                StartCoroutine(PlayAudio());
                break;
        }

        Tekspertan.text = pertanyaan.InfoPertan;

        List<string> Listjawaban = ShuffleList.ShuffleListItems<string>(pertanyaan.opsis);

        for (int i = 0; i < opsis.Count; i++)
        {
            opsis[i].GetComponentInChildren<TMP_Text>().text = Listjawaban[i];
            opsis[i].name = Listjawaban[i];
            opsis[i].image.color = normalCOl;
        }

        dijawab = false;
    }

    IEnumerator PlayAudio()
    {
        if(pertanyaan.tipePertan == TipePertan.AUDIO)
        {
            Audiopertan.PlayOneShot(pertanyaan.PertaClip);

            yield return new WaitForSeconds(durasiAudio + 0.5f);

            StartCoroutine(PlayAudio());
        }
        else
        {
            StopCoroutine(PlayAudio());
            yield return null;
        }
    }

    void TempatMedia()
    {
        Gambarpertan.transform.parent.gameObject.SetActive(true);
        Gambarpertan.transform.gameObject.SetActive(false);
        Audiopertan.transform.gameObject.SetActive(false);
        Videopertan.transform.gameObject.SetActive(false);
    }

    
    public void Onclick(Button btn)
    {
        
        if (!dijawab)
        {
            dijawab = true;
            bool val = quizManager.Jawaban(btn.name);

            if (val)
            {
                btn.image.color = JbnrCOl;
            }
            else
            {
                btn.image.color = JslhCOl;
                
                // Added by Ady
                var correctBtn = GetCorrectButton();
                if (correctBtn != null)
                    correctBtn.image.color = JbnrCOl;
            }
        }
    }
    
    // Added by Ady
    public Button GetCorrectButton()
    {
        string correctAnswer = quizManager.GetCorrectAnswer();
        return opsis.Find(btn => btn.name == correctAnswer);
    }
}

After following @Ady_M guide i was able to make it and this is how it looks like

( i want to put it in the main post but i keep getting 422 error )