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 konanxiao · Jun 04, 2013 at 08:01 AM · mathparseequationgui.textfield

equation game question need help

Hi,

I make a simple Unity3D math game script that user must enter correct answer for the math problem given. This is the script I used. I just attached this scrip to an Empty game object on a blank scene. Try this to figure out what I get.

 #pragma strict
 var a : int;                    //Define variable a as int
 var b : int;                    //Define variable b as int
 var c : int;                    //Define variable c as int
 var stringAnswer : String;        //Define the answer as string input
 var numAnswer : int;            //Define answer in numeric as int
 
 function Start()
 {
     a = Random.Range(0, 9);        //Get random value for a
     b = Random.Range(0, 9);        //Get random value for b
     c = a + b;                    // c = a + b
 }
 
 function OnGUI()
 {
     GUI.Box(Rect(Screen.width/2 - 200, Screen.height/2 - 5, 400, 40), a + " + " + b + " = ");                 //Create a box show the equation
     stringAnswer = GUI.TextField(Rect(Screen.width/2 + 30, Screen.height/2, 40, 30), stringAnswer, 2);         //Create a text field that player input the answer, wether it numeric or text.
     
     numAnswer = int.Parse(stringAnswer);                //Convert the string answer to int value
     
     if(numAnswer == c)                        //Compare the int value of answer with c
     {
         print("Answer is Correct !!");                //If answer is equal c, tell in terminal that the answer is correct. In future will be a GUI text. 
     }
     else
     {
         print("Answer is Wrong !!");                //If answer is not equal c, tell in terminal that the answer is wrong. In future will be a GUI text. 
     }
 }

then when I run, I get this error message :

FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) test.OnGUI () (at Assets/test.js:20)

That is error in parsing text to integer value.

I use Unity3D v.3.5.5f3 on Windows7 64.

Are there someone could help me correcting this script?

Thank You very much for Your help.

Comment
Add comment · Show 1
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
avatar image syclamoth · Jun 04, 2013 at 08:05 AM 0
Share

The problem is that you are not checking that the inputted string is of the correct format before attempting to parse it. When the field is initialised, it will contain an empty string- which is an invalid format, because it cannot be converted into an integer! I recommend surrounding the 'int.Parse' function with a try-catch block, and handling the error manually. I'm not entirely certain of the correct syntax for that in JavaScript, so you'll have to ask someone more knowledgeable than me.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ashkan_gc · Jun 04, 2013 at 08:39 AM

Use the int.TryParse instead of Parse, If it returns true then it means it could parse the text.

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

16 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

Related Questions

How do I make this XML file parser work in Unity 1 Answer

Writing maths expressions 1 Answer

Need help with easing effects which respect Time.deltaTime, MATH 0 Answers

Any Ideas on how to solve this? 1 Answer

Optimising projectile continuously calculated impact point 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