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 Ian-McCleary · Mar 27, 2015 at 02:56 AM · uiinputvariablefunctions

How do I add/multiply input field boxes?

I am creating an app that needs to take the variable input from one input field UI box, then take the variable from the other box, and multiply them together to create an answer that shows up on the screen when a button is clicked.

I am unsure of the proper calling for the functions because when i look at the input field functions this website comes up: http://docs.unity3d.com/ScriptReference/UI.InputField.html It has everything i need except i cant find basic functions such as addition and multiplication as well as how to call an input field to be used.

If you have some sample code of how to do this, it would be much appreciated. JS is preferred for me but i know C# as well.

If you have any questions please feel free to ask. I may have not made my question clear enough.

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
Best Answer

Answer by DiegoSLTS · Mar 27, 2015 at 03:28 AM

InputFields contain a string text even if you set it to only admit numbers, so you have to start from there.

Also, InputFields are just that, a field where you can input something, adding and multiplying it's content is not part of their functionality.

To do math operations you have to take the text string from both InputFields (the "text" property) then parse an integer or decimal value from it (https://msdn.microsoft.com/en-us/library/vstudio/bb397679%28v=vs.110%29.aspx) and then make the operation with the common math operators.

Once you have the result you have to turn the number value into a string again (the ToString method) and set it as the "text" property of the Text component where you want to display the result.

A quick sample code, I haven't tested it:

 using UnityEngine;
 using UnityEngine.UI;
 using System;
 
 public class InputFieldOperations : MonoBehaviour {
 
     public InputField Field1;
     public InputField Field2;
     public Text Result;
 
     public void Sum() {
         int a = Convert.ToInt32(Field1.text);
         int b = Convert.ToInt32(Field2.text);
         int c = a+b;
         Result.text = c.ToString();
     }
 
 
     public void Product() {
         int a = Convert.ToInt32(Field1.text);
         int b = Convert.ToInt32(Field2.text);
         int c = a*b;
         Result.text = c.ToString();
     }
 
 }

Set that as a component of something, drag and drop the inputfields and the text and set the OnClick callback of a button to call the Sum method, and the OnClick callback of another button to call the Product method.

Comment
Add comment · Show 5 · 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
avatar image Ian-McCleary · Mar 27, 2015 at 04:05 AM 0
Share

Would you recommend i place the main script like this on an empty game object with the canvas/all ui inside? or just place it on the canvas?

avatar image Ian-McCleary · Mar 27, 2015 at 04:59 AM 0
Share

I decided to place it on an empty game object. 1 last thing. Unity doesnt recognize "Convert" in the context used and should i rename "Field1" and "Field2" to my own text box names so it will call from them?

avatar image DiegoSLTS · Mar 27, 2015 at 02:21 PM 0
Share

I just tested that code and it's working for me. I can't recomend where to put that script, I added it to the Canvas for a quick test but I guess it would be better in an empty game object like you did or in a panel that contains evenrything. Or maybe it shouldn't be a component on it's own and should ins$$anonymous$$d be part of another bigger script that does other stuff.

Actually, I just wanted to give you a $$anonymous$$imum example of how to do operations between InputFields. You might even put that script on the Text that shows the result, make the Result reference private and the get Text component from the game object ins$$anonymous$$d.

About rena$$anonymous$$g variables, yes, name them however you think will look cleaner in your code. $$anonymous$$aybe "LeftOperand" and "RightOperand" or something less abstract if your problem has a name for what those numbers mean.

avatar image Ian-McCleary · Mar 28, 2015 at 01:11 AM 0
Share

I have a question about the 'Result.text = c.ToString();'

Does this make it so that as soon as the values are put into the boxes it automatically updates? I am using a button to calculate the two variables once they are put into the boxes. How would i implement that? Thank you so much this far. Im sorry im pretty new to the UI.

avatar image DiegoSLTS · Mar 30, 2015 at 12:38 PM 0
Share

InputFields have 2 events that are triggered automatically, one is "On value change" that's triggered every time you press a key, and the other is "End Edit", triggered when you press enter or click outside the InputField.

You can setup callbacks to be called in those events, just like you do for buttons.

If you set a same function on the "On value change" of both InputFields that code will be executed automatically whenever you write some value. Note it will also execute when you erase values, so you have to add some checks or you might have some exceptions trying to convert an empty string into a value.

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

21 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

Related Questions

UI InputField text sometimes invisible? 1 Answer

How to change scoreInputField to get a score from another script/Scene 0 Answers

Showing UI image when inside a trigger and then pressing a button 2 Answers

Best way to show variable values on UI 1 Answer

Why does the keyboard control what button is selected? 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