Hello,
I’m essentially trying to create this for a maths game. I’m trying to use the Unity Toggle (checkboxes) to store a list of numbers selected into an array, so the numbers provided in sums are based on the ones chosen by the checkboxes.
The sum will display on the screen when the train stops at a station, then proceed to generate a new number at the next stop.
Anyway, I’m pretty bad at coding an have evidently missed something/messed up somewhere; whenever the train stops at the station; Unity will crash so the maths sum is never displayed on screen.
Any suggestions to fix this will be greatly appreciated. Here’s the code I’ve created at the moment.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIScript : MonoBehaviour {
GameObject Train;
private bool Toggle1 = false;
private bool Toggle2 = false;
private bool Toggle3 = false;
private bool Toggle4 = false;
private bool Toggle5 = false;
private bool Toggle6 = false;
private bool Toggle7 = false;
private bool Toggle8 = false;
private bool Toggle9 = false;
private bool shouldContinue = true;
public Text Text_AnswerField;
public Text Text_SumField;
public Text Text_NumberField1;
public Text Text_NumberField2;
public bool[] ToggleState;
public bool atStation;
int[] values;
void Start()
{
ToggleState = new bool[9];
Train = GameObject.FindGameObjectWithTag ("Player");
}
void OnGUI()
{
ToggleState.SetValue (Toggle1, 0);
ToggleState.SetValue (Toggle2, 1);
ToggleState.SetValue (Toggle3, 2);
ToggleState.SetValue (Toggle4, 3);
ToggleState.SetValue (Toggle5, 4);
ToggleState.SetValue (Toggle6, 5);
ToggleState.SetValue (Toggle7, 6);
ToggleState.SetValue (Toggle8, 7);
ToggleState.SetValue (Toggle9, 8);
if (atStation)
{
Train.GetComponent<TrainScript>().isStopped(false);
atStation = false;
}
}
public void AskQuestion()
{
int Number1;
int Number2;
int CorrectResult;
int Incorrect1;
int Incorrect2;
GameObject.Find ("UI").GetComponent<UIScript> ().atStation = true;
Number1 = GetNewNumber();
Number2 = GetNewNumber();
CorrectResult = Number1 + Number2;
Incorrect1 = CorrectResult - 1;
Incorrect2 = CorrectResult + 1;
Text_SumField.text = Number1 + "+" + Number2;
Text_AnswerField.text = ""+CorrectResult;
Text_NumberField1.text = ""+Incorrect1;
Text_NumberField1.text = ""+Incorrect2;
}
public int GetNewNumber()
{
bool shouldContinue = true;
do
{
int Rand = Random.Range (1, 10);
if(ToggleState[Rand-1] == true)
{
return Rand;
shouldContinue = false;
}
}while(shouldContinue == true);
return 0;
}
}