Working in Unity: Tip calculator

Jake Boere
2 min readApr 27, 2021

If you want to get more familiar with variables and how they can be used then try creating a tip calculator. For that you will need some variables as well as making some math equations. to start lets create the variables we will need a variable for the bill, the tip, and the total amount you will owe once it is all calculated.

For the variables you can hard code the bill and tip in if you want but its a lot easier to just leave it if we want to change the values later on so for this example I will just hard code the bill and leave the tip. So your code should end up looking like the example shown below:

For the bill float would work too but since I know the bill is just 40 I used an int.

After the variables are all set up we will want to write the equation in void start. To start we need to figure out the total amount of the tip so we create a new variable in void start. make sure the variable is different from your others or they won’t work. Then multiply the bill by tip amount divided by 100 to get the percentage of the tip.

After we’ve calculated that we can figure out the total amount of the bill and to do that we will right the variable for the total is equal to the bill plus the top total and you should have a working tip calculator.

If you want to make sure it works we will need to write another Debug.Log to return the answer to the console if you want you can check for the other variables as well like I have done in the example below and once you run your game you should see a message in your console.

--

--

Jake Boere

I am hard working Game development student learning as much about game development and unity as possible.