- Home /
Create multiple math problems with arrays? Help with arrays
Hey there everyone. So I'm working on a multiplication game for my brother who has trouble in that area and I could use some help myself.
Basically, I'm trying to set up a system that would allow me to set the number of math problems in the inspector. Then within those problems I'm able to set the values of those math problems. There would only be three numbers in each problem; TopNumber, BottomNumber, and AnswerNumber.
I've managed to get that top part working with what I want to say is called an array. What I really need help with is setting the UI text to match the current math problem's variables. I also need help with finding out which problem the player is currently on.
The topNumberText and the bottomNumberText would match the current problem's variables.
I've posted my current code below and I've commented what I want it to do. I've also included a picture of my Inspector if that helps. If you could help me out that would be amazing! If you need more information or something I would gladly do so.
//THE MATH PROBLEM STUFF
[System.Serializable] //Allows me to mess with values in Inspector
public class NumberOfProblems //This is the name of the class
{
public string name; //The name of each individual problem ex. "Problem 1" and so on
public int topNumber; //The top # in the math problem, also known as the first #
public int bottomNumber; //The bottom # in the math problem, also known as the second #
public int answer; //The answer to top# & bottom# being multiplied
}
public NumberOfProblems[] problems; //Allows me to set the number of problems
public int currentProblem = 0; //I'm assuming I'm going to need this to find out what problem I'm on
void SetProblem() //Supposed to set the Texts to the currentProblems variables
{
//Not really sure what to put here
}
void NextProblem() //If the player inputs the correct answer, move onto the next problem
{
//I'm not even sure if I need this
}
void ProblemsFinished() //If the Player completed all of the problems
{
print("All problems are finished, unlock the door"); //Just some filler stuff
}
//THE UI TEXT STUFF
public InputField answerInputField; //The InputField where the Player can type
public Text topNumberText; //The Text that's supposed to be the top number
public Text bottomNumberText; //The Text that's supposed to be the bottom number
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player") //Making sure the Player is what is in the trigger
{
//Just selects the InputField so the Player can type the answer
answerInputField.Select();
answerInputField.ActivateInputField();
}
}
private void Update()
{
//Here it would check if what the Player typed is equal to the currentProblem's answer, then you would move on
//I'm not too sure how to do this to be honest
//It would also check if all of the problems were finished, then it would call the ProblemsFinished function
}
Answer by Namey5 · Nov 12, 2020 at 01:21 AM
Let's start off with the SetProblem() function - all you really need to do here is to set the 'text' variable of both Text components to the values of the current question (and maybe clear the input field while you're there);
void SetProblem ()
{
//Clear the input field
answerInputField.text = "";
//Then set the problem labels to the current question
topNumberText.text = problems[currentProblem].topNumber.ToString();
bottomNumberText.text = problems[currentProblem].bottomNumber.ToString();
}
From there, I would leave out the NextProblem() function as it is a bit specific and instead handle that when checking the answer. Speaking of which, there are a few ways of doing this. I'll put it it a separate function in case you want a UI button to check the answer, but for starters we can just check answers when the Enter key is pressed;
private void Update ()
{
//If the player pushes the enter key, check their answer
if (Input.GetKeyDown (KeyCode.Return))
{
CheckAnswer ();
}
}
public void CheckAnswer ()
{
//We can use the int.TryParse() function to extract an integer out of a string - if the string isn't in the right format (i.e. isn't a number) then the function will return false
if (int.TryParse (answerInputField.text, out int playerAnswer))
{
//Check the player's answer
if (playerAnswer == problems[currentProblem].answer)
{
//The correct answer was input, so move on to the next problem
print ("Correct answer!");
currentProblem++;
//Check if we have finished all the problems, if not set the next one
if (currentProblem >= problems.Length)
{
ProblemsFinished ();
}
else
{
SetProblem ();
}
}
else
{
//The player's answer was wrong
print ("Wrong answer, try again!");
}
}
else
{
//We couldn't get a valid number out of the input, probably due to incorrect formatting
print ("Please type a valid number.");
}
}
If you want to start a new set of problems, just make sure to reset your 'currentProblem' variable to 0.