Question by
aslamdhoni · May 18, 2020 at 06:03 AM ·
unity 5quiz
quiz game
hi im really new in unity, please help finish my college finale project, so i want my quiz game is stop and show score when user already answer 15 question. i use brackleys source code in below. thank you so much
public Pertanyaan[] pertanyaan; private static List unansweredQuestions;
private Pertanyaan currentQuestion;
[SerializeField] // menampilkan variable private di inspector
private Text soalText;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
[SerializeField]
private float timeBetweenQuestion = 1f;
// mengecek pertanyaan yang sudah dijawab
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = pertanyaan.ToList<Pertanyaan>();
}
SetCurrentQuestion();
}
// mengacak pertanyaan yang muncul
void SetCurrentQuestion()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
soalText.text = currentQuestion.soal;
//mengatur animasi ketika user menjawab
if(currentQuestion.jawaban)
{
trueAnswerText.text = "KAMU BENAR +5";
falseAnswerText.text = "KAMU SALAH +0";
}
else
{
trueAnswerText.text = "KAMU SALAH +0";
falseAnswerText.text = "KAMU BENAR +5";
}
}
//memberi waktu sebelum ganti soal
IEnumerator TransitionToNextQuestion()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(timeBetweenQuestion);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
// memilih jawaban benar atau salah
public void UserSelectTrue()
{
animator.SetTrigger("True");
if (currentQuestion.jawaban)
{
Debug.Log("Correct");
Score.scoreValue += 5;
}
else
{
Debug.Log("Wrong ");
}
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFalse()
{
animator.SetTrigger("False");
if (!currentQuestion.jawaban)
{
Debug.Log("Correct");
Score.scoreValue += 5;
}
else
{
Debug.Log("Wrong ");
}
StartCoroutine(TransitionToNextQuestion());
}
}
Comment
Answer by unity_qCJMzrGkwMTK9Q · Jun 22, 2020 at 03:05 PM
Add int score = 0 and increase whenever player answers question correctly. Then add a panel with text over everything on screen and enable it when score is 15.
// use this to acess end screen
public gameObject EndScreen;
//put this in start method to hide End screen
EndScreen.SetActive(false);
//Use this to detect when player have enough points
if (Score >= 15)
{
EndScreen.SetActive(false);
}
I hope it helps!