My script for my buttons doesn't seem work? HELP!
Hi, I'm a beginner in programming.
I want to make a quiz game. Have a text (for question) and 2 buttons (options for answer). What I want from my code is, every time I click one of my buttons (right or wrong), the question changes to next question in my list. The score is functioning perfectly, but the question and buttons don't want to change. So, I wonder if there's something wrong with my code?
Here's my code :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class RandomQuestion : MonoBehaviour {
// Use this for initialization
private List<string[]> questions = new List<string[]>();
private List<int> answerOrder = new List<int>(new int[] { 1, 2 });
UnityEngine.UI.Text Question;
public Button aButton1;
public Button aButton2;
public Text test;
void DrawInfo()
{
Rect rect = new Rect(500, 100, 400, 200);
Question = GetComponent<UnityEngine.UI.Text>();
Question.text = questions[0][0];
aButton1.gameObject.GetComponentInChildren<Text>().text = questions[0][answerOrder[0]];
aButton2.gameObject.GetComponentInChildren<Text>().text = questions[0][answerOrder[1]];
if (aButton1.IsActive())
{
aButton1.onClick.AddListener(delegate { CheckAnswer(answerOrder[0]); });
}
if(aButton2.IsActive())
{
aButton2.onClick.AddListener(delegate { CheckAnswer(answerOrder[1]); });
}
}
private void CheckAnswer(int answer)
{
if (answer == 1)
{
//handle correct answer
GameControl.control.score += 10;
}
else {
//handle wrong answer
GameControl.control.score -= 10;
}
NextQuestion();
}
void OnGUI()
{
test.text = questions.Count.ToString();
//this is just to protect from an empty list... you will need better win/lose conditions.
if (questions.Count > 0)
{
DrawInfo();
}
else
{
GameControl.control.Tutorial();
}
}
void Start()
{
// String order: question, correct, wrong
questions.Add(new string[] { "What in ?", "aa", "bb" });
questions.Add(new string[] { "Which of ?", "cc", "dd" });
questions.Add(new string[] { "Which of these ?", "ee", "ff" });
Shuffle(questions);
Shuffle(answerOrder);
GameControl.control.AddTutorials();
}
void NextQuestion()
{
questions.RemoveAt(0);
Shuffle(answerOrder);
}
static readonly System.Random rng = new System.Random();
public static void Shuffle<T>(IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
Sorry for my bad English and thank you.
Comment
Your answer
Follow this Question
Related Questions
How to create an event trigger script for intercepted events 0 Answers
Loading Game Crash 1 Answer
Multiplayer Hiding Layer just for Local Player 0 Answers
How to change GUI label size 1 Answer
Connect C# script with GuiText 1 Answer