Unity Game Programming Tutorials
Learning how to build a basic calculator in Unity
Hi there! I hope that you have missed me. Well, maybe not me, but at least my tutorials ;) If that is true, you will be glad to discover another code-packed tutorial, which will help us create a basic calculator.
We will discuss enums, learning how to create fully functional drop-down menus that are displayed in Unity's Inspector. Then, we will use an enum to pick the desired math operation. We'll end up having a simple calculator, but nothing prevents you from adding as many math functions as you want to it, of course.
Let us start our new programming experiment by creating a fresh C# script. We will name it "Calculator", and then double click it to open it inside Visual Studio. Then, let's add the lines of code below to your newly created script.
I know that this code snippet is a bit more complex, but don't worry; we will break it down in smaller pieces and we'll discuss them thoroughly.
public float firstNumber;
public float secondNumber;
public float myResult;
The variables above are public, because we want to see them in the inspector. The first two store the numbers we're working with, while the last one will store the result.
Let us explore the next few lines of code:
public enum calculatorOperation
In C#, enums are data types which can be used to assign names to numbers (integer constants). In other words, we use enums to refer to numbers by assigning those numbers some user-friendly names, and thus make our code more readable and easier to manage.
So, rather than using calculatorOperation.0 to refer to the first enum member, we can use calculatorOperation.Addition, get it? The same goes for the rest of the elements; calculatorOperation.Subtraction is in fact calculatorOperation.1, and so on.
By doing this, we can give descriptive names to the math operations which are used for our calculator.
Now that we've built our enum, we can create an instance of it which can be used in our code, and the line of code below does just that. By making the enum public, we can access its properties in the Inspector, the way you've seen them in the image above.
public calculatorOperation operation;
The last lines of code in our script take care of the math calculus:
if (operation == calculatorOperation.Addition)
myResult = firstNumber + secondNumber;
if (operation == calculatorOperation.Subtraction)
myResult = firstNumber - secondNumber;
if (operation == calculatorOperation.Multiplication)
myResult = firstNumber * secondNumber;
if (operation == calculatorOperation.Division)
myResult = firstNumber / secondNumber;
The code is very simple; we determine the desired operation depending on the enum value which was chosen in the Inspector, and then we use it in conjunction with the two input numbers. Since myResult is a public variable, its value will be displayed in the Inspector as well.
If you're feeling adventurous, you may want to try and crash the program, dividing a number by zero. Fortunately, the good Unity developers have already thought about that, so our program will display "Infinity" as a result if you try to do that.
I hope that you've learned a lot this time, and you look forward to the next C# Unity programming tutorial. I hope to see you again soon!