Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlejandroBoss10 · Nov 11, 2020 at 09:09 PM · arraytextmathsetfloat

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.

alt text

 //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
 }


math-probelm-inspector.png (45.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change which camera is active based on text dialogue? 2 Answers

Coloring separate text in string array 1 Answer

What's a more efficient way to sort an array into multiple groups? 1 Answer

How can I pick 3 random Gameobjects from an array and then shrink them to a point? 2 Answers

How to destroy numbers after solving them? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges