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 3 · Mar 11, 2013 at 06:12 PM · calculationsubtractingdivision

How do make a basic Script for a calculator?

Well I want to make a simple calculator app for iOS, and all I've got so far is this, I'm stumped, you know how a calculator works, where if you click the number 2 and then 3 it makes 23, well I got it to the point where if you click 2 and 3 it makes 6. Anyways, any help would be appricated, where do I go from here? I've got experience with unity and unityscript, but this is forign to me. Thanks:

 private var hit : RaycastHit;
 private var ray : Ray;
 var results1 : GUIText;
 var input1 = 0;
   function FixedUpdate () {
 results1 == input1;
     if(iPhoneInput.touchCount == 1) {
         ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position);
         Debug.DrawLine(ray.origin,ray.direction * 10);
         if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
             Debug.Log(hit.transform.name);//Object you touched
                             if(hit.transform.name == "number 1"){
                             input1
             } 
                             if(hit.transform.name == "number 2"){
             } 
                             if(hit.transform.name == "number 3"){
             } 
                             if(hit.transform.name == "number 4"){
             } 
                             if(hit.transform.name == "number 5"){
             } 
                             if(hit.transform.name == "number 6"){
             } 
                             if(hit.transform.name == "number 7"){
             } 
                             if(hit.transform.name == "number 8"){
             } 
                             if(hit.transform.name == "number 9"){
             } 
                             if(hit.transform.name == "number 0"){
             } 
                             if(hit.transform.name == "number +"){
             } 
                             if(hit.transform.name == "number -"){
             } 
                             if(hit.transform.name == "number *"){
             } 
                             if(hit.transform.name == "number /"){
             } 
                             if(hit.transform.name == "number ac"){
             } 
                             if(hit.transform.name == "number ="){
             } 
         }
     }
 } 
Comment
Add comment · Show 3
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 3 · Mar 11, 2013 at 11:01 PM 0
Share

alright Ill check it out in a bit, thanks

avatar image 3 · Mar 12, 2013 at 04:34 AM 0
Share

unfortunately I don't know C#, I'll try to figure it out though. Thanks,

avatar image 3 · Mar 14, 2013 at 12:12 AM 0
Share

I dont know why, but even with some editing Nothing really happens, the script on its own doesn't seem to do much. Besides attaching it to the main camera is there anything else I should do?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Mar 11, 2013 at 06:28 PM

The code here does not add two numbers yet. Maybe you gave us a cut down version. Anyway to solve your input of '23', calc your result input this:

 if(hit.transform.name == "number 3"){
   inputCurr = inputCurr * 10 + 3;
             } 
 if(hit.transform.name == "number 4"){
   inputCurr = inputCurr * 10 + 4;
             } 

If the user hits any of the '+-*/' symbols, then inputCurr becomes input1 (or input2 if it is the second number).

Note if you are going to make a real calculator you have to handle the order of operations. For example if the user types in 2 + 3 * 6 the result should be 20, not 30 since the multiplication is done before the addition. Usually this is solved by building a stack data structure and putting calculations on the stack until they can be processed.

Comment
Add comment · Show 7 · 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 3 · Mar 11, 2013 at 07:09 PM 0
Share

I'll try that out and let you know And I know it doesn't add two numbers, I cut that out after it wasn't working

avatar image 3 · Mar 11, 2013 at 07:43 PM 0
Share

doesn't seem to help, sorry.

avatar image robertbu · Mar 12, 2013 at 01:18 AM 0
Share

If you post your script back to the list with the changes, I'm the list can figure out what needs to be done.

avatar image 3 · Mar 12, 2013 at 04:21 AM 0
Share

I dont know what you mean. I can't figure this out for the life of me. I guess I just need a superior being to help me out. Thanks again.

avatar image robertbu · Mar 12, 2013 at 05:27 AM 0
Share

Take the script you have written so far which includes the code you did not post originally and the code you added based on my answer and post it back to the list either as an edit to your question or as comment.

Show more comments

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

11 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

Related Questions

Subtracting random numbers instead of 1 1 Answer

What do I add to this script, so if you clicked the button ex. 30 times, a gui.box will appear? 1 Answer

Terrain Height Map Generated from raycast array[Help] 0 Answers

Matrix Multiplication 1 Answer

Material adding and subtraction 0 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