Here's an example of how the code could be implemented in C# for a quiz game in Unity:
using UnityEngine;
using UnityEngine.UI;
public class QuizGame : MonoBehaviour
{
public Text questionText;
public Button[] answerButtons;
public Text scoreText;
public Text livesText;
public Text timerText;
public Text questionNumberText;
public Button muteButton;
public Button exitButton;
public Button leaderboardButton;
private int questionIndex = 0;
private int score = 0;
private int lives = 3;
private float timer = 20f;
private bool isMuted = false;
private bool isGameOver = false;
private string[] questions = {
"Question 1?",
"Question 2?",
"Question 3?",
// add more questions here
};
private string[] correctAnswers = {
"Correct Answer 1",
"Correct Answer 2",
"Correct Answer 3",
// add more correct answers here
};
private string[][] incorrectAnswers = {
new string[] { "Incorrect Answer 1", "Incorrect Answer 2", "Incorrect Answer 3" },
new string[] { "Incorrect Answer 1", "Incorrect Answer 2", "Incorrect Answer 3" },
new string[] { "Incorrect Answer 1", "Incorrect Answer 2", "Incorrect Answer 3" },
// add more sets of incorrect answers here
};
private void Start()
{
DisplayNextQuestion();
}
private void Update()
{
if (timer > 0 && !isGameOver)
{
timer -= Time.deltaTime;
timerText.text = Mathf.Round(timer).ToString();
}
else if (!isGameOver)
{
EndGame();
}
}
public void AnswerButtonClicked(int buttonIndex)
{
if (answerButtons[buttonIndex].GetComponentInChildren<Text>().text == correctAnswers[questionIndex])
{
score += 10;
}
else
{
lives -= 1;
}
DisplayNextQuestion();
}
public void MuteButtonClicked()
{
isMuted = !isMuted;
// implement audio mute/unmute logic here
}
public void ExitButtonClicked()
{
// implement code to exit the game here
}
public void LeaderboardButtonClicked()
{
// implement code to show leaderboard here
}
private void DisplayNextQuestion()
{
if (questionIndex < questions.Length)
{
questionText.text = questions[questionIndex];
ShuffleAnswers();
questionNumberText.text = (questionIndex + 1).ToString() + "/" + questions.Length.ToString();
scoreText.text = "Score: " + score.ToString();
livesText.text = "Lives: " + lives.ToString();
timer = 20f;
}
else
{
EndGame();
}
}
private void ShuffleAnswers()
{
string[] answers = new string[4];
answers[0] = correctAnswers[questionIndex];
for (int i = 1; i < 4; i++)
{
int randomIndex = Random.Range(0, incorrectAnswers[questionIndex].Length);
answers[i] = incorrectAnswers[questionIndex][randomIndex];
// Ensure unique incorrect answers
if (i > 1 && answers[i] == answers[i - 1])
{
i--;
}
}
// Randomize the order of the answers
for (int i = 0; i < 4; i++)
{
int randomIndex = Random.Range(i, 4);
string temp = answers[i];
answers[i] = answers[randomIndex];
answers[randomIndex] = temp;
}
// Assign shuffled answers to button texts
for (int i = 0; i < 4; i++)
{
answerButtons[i].GetComponentInChildren<Text>().text = answers[i];
}
}
private void EndGame()
{
isGameOver = true;
// implement code to handle game over and display final score, etc.
}
}
Make sure to attach this script to a GameObject in your Unity scene and assign the necessary UI elements (Text, Buttons) to their corresponding variables in the script through the Inspector. Additionally, don't forget to implement the logic for the mute button, exit button, and leaderboard button based on your specific requirements.