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