I just wanted to get some criticism on my programming to make me better. I made a game in Unity, just a simple question and answer game.
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using UnityEditor.VersionControl;
using System.Threading;
using System;
[System.Serializable]
class Question
{
public string question;
public bool answer;
public Question(string question, bool answer)
{
this.answer = answer;
this.question = question;
}
public string getQuestion()
{
return question;
}
public void setQuestion(string msg)
{
question = msg;
}
public bool getAnswer()
{
return answer;
}
}
class NoMoreQuestionsException : Exception
{
public NoMoreQuestionsException()
{
}
}
public class GameManager : MonoBehaviour {
Question currentQuestion;
[SerializeField]
float buttonMoveSpeed;
int points = 0;
bool isPlayerCorrect;
bool changedAfterAnswer = false;
bool moveComplete = false;
int answer = 0;
int currentQuestionNumber;
[SerializeField]
float waitBetweenQuestions;
//List of questions
[SerializeField]
Question[] questions;
List<Question> unansweredQuestions = new List<Question>();
//Variables
[SerializeField]
GameObject questionText;
[SerializeField]
GameObject falseButton;
[SerializeField]
GameObject trueButton;
[SerializeField]
Text trueAnswerText;
[SerializeField]
Text falseAnswerText;
[SerializeField]
Text pointsText;
public void Answered (bool playerAnswer)
{
if (playerAnswer)
{
answer = 1;
} else
{
answer = -1;
}
}
void NewRound()
{
if (unansweredQuestions.Count < 2)
{
Debug.Log("Error! No more questions to answer!");
throw new NoMoreQuestionsException();
return;
}
//Remove old question
unansweredQuestions.RemoveAt(currentQuestionNumber);
//Check if there are any more questions to answer
Debug.Log("New Round!");
//Generate new question number
System.Random ran = new System.Random();
currentQuestionNumber = (ran.Next(unansweredQuestions.Count - 1));
//Set current question
currentQuestion = unansweredQuestions[currentQuestionNumber];
//Set question text to the new question
Text txt = questionText.GetComponent<Text>();
txt.text = currentQuestion.question;
//Reset players result
answer = 0;
//Set the buttons back to centre
falseButton.transform.position = new Vector3(1012.5f, 218.5f, -107.9f);
trueButton.transform.position = new Vector3(283.5f, 218.5f, -107.9f);
changedAfterAnswer = false;
}
void Start () {
//Load points
System.Random ran1 = new System.Random();
points = ran1.Next(100);
pointsText.text = points.ToString();
//Se the level
//Set unaswered questions to the main questions
unansweredQuestions = questions.ToList();
//Check if there are any questions to answer
if (unansweredQuestions.Count < 1)
{
Debug.Log("Error! No questions to answer!");
}
Debug.Log("New Round!");
//Generate new question number
System.Random ran = new System.Random();
currentQuestionNumber = (ran.Next(unansweredQuestions.Count - 1));
//Set current question
currentQuestion = unansweredQuestions[currentQuestionNumber];
//Set question text to the new question
Text txt = questionText.GetComponent<Text>();
txt.text = currentQuestion.question;
//Reset players result
answer = 0;
//Set the buttons back to centre
falseButton.transform.position = new Vector3(1012.5f, 218.5f, -107.9f);
trueButton.transform.position = new Vector3(283.5f, 218.5f, -107.9f);
changedAfterAnswer = false;
Debug.Log("Question Number: " + currentQuestionNumber);
}
void moveFalse()
{
if (falseButton.transform.position.x > 1330)
{
moveComplete = true;
return;
}
Vector3 pos = falseButton.transform.position;
pos.x += buttonMoveSpeed;
falseButton.transform.position = pos;
moveComplete = false;
}
void moveTrue()
{
if (trueButton.transform.position.x < -25)
{
moveComplete = true;
return;
}
Vector3 pos = trueButton.transform.position;
pos.x -= buttonMoveSpeed;
trueButton.transform.position = pos;
moveComplete = false;
}
//Check if users answer is correct
bool isCorrect(bool playerAnswer)
{
if (currentQuestion.getAnswer() == playerAnswer)
{
points += 1;
pointsText.text = points.ToString();
return true;
}
return false;
}
void setAnswerText(bool answer, string text)
{
if (!answer)
{
trueAnswerText.text = text;
} else
{
falseAnswerText.text = text;
}
}
IEnumerator StartWait()
{
yield return new WaitForSeconds(waitBetweenQuestions);
WaitForNewQuestion();
}
void WaitForNewQuestion()
{
NewRound();
startedTimer = false;
}
bool startedTimer = false;
void Update () {
//Checking if the player has clicked a button
if (answer == 0)
{
return;
}
//Check is user has pressed the true button.
if (answer == 1)
{
//Check if player is correct and change the answer text to show.
if (!changedAfterAnswer)
{
if (isCorrect(true))
{
setAnswerText(true, "Correct!");
}
else
{
setAnswerText(true, "Incorrect");
}
changedAfterAnswer = true;
}
//Move the false button
moveFalse();
if (!startedTimer)
{
Debug.Log("STARTED WA8T");
startedTimer = true;
StartCoroutine(StartWait());
}
}
//Check is user has pressed the false button.
if (answer == -1)
{
//Check if player is correct and change the answer text to show.
if (!changedAfterAnswer)
{
if (isCorrect(false))
{
setAnswerText(false, "Correct!");
}
else
{
setAnswerText(false, "Incorrect");
}
changedAfterAnswer = true;
}
//Move the true button
moveTrue();
if (!startedTimer)
{
Debug.Log("STARTED WA8T");
startedTimer = true;
StartCoroutine(StartWait());
}
}
}
}