- Home /
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 ="){
}
}
}
}
unfortunately I don't know C#, I'll try to figure it out though. Thanks,
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?
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.
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
If you post your script back to the list with the changes, I'm the list can figure out what needs to be done.
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.
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.