# An Artist's Guide to C# in Unity3D: Operators

### This article is part of an ongoing series for code-leaning artists who want to learn Unity 3D. In this article I introduce you to operators and how to use them on different data types.

*This article is part of an ongoing series for code-leaning artists who want to learn Unity 3D. In this article I introduce you to operators and how to use them on different data types.*

*Read Previous: Common data types and variables*

Operators provide actions such as value manipulation, logical comparisons or mathematical calculations that may be applied to data types. Each operation is usually represented by its approximate conventional mathematical symbol. E.g., the plus (+) and minus (-) for addition and subtraction, respectively.

C-sharp comes with several built-in operators but we’ll cover just the operators in these four groups

Arithmetic Operators

Logic Operators

Relational Operators

Assignment Operators

**Arithmetic Operators**

As the name implies, these operators apply arithmetic calculations on numeric values. Arithmetic operators also work on char values because each character is represented by its numeric ASCII value under the hood. The only arithmetic operator which works on string values is the plus (+) operator which concatenates the string operands together.

Here are the arithmetic operators in order of precedence

**Logic Operators**

These operators perform simple logic algebra on boolean values. Here they are in precedence order.

**Relational Operators**

These operators perform quantitative comparisons between values such as “greater than,” “less than,” or “equal to,” and return true or false.

**Assignment Operators**

We have seen the basic assignment operator (=) already. It's the basic way of assigning a value to a particular variable identifier (e.g., name = “John”). The other five assignment operators do the same thing except they perform some sort of arithmetic operation on the value before assigning the result to a variable.

Here are the assignment operators.

**Try it out**

Now that we’ve learned about operators, we have enough knowledge to calculate some simple equations.

Let’s revisit some of our elementary school word problems.

Take a look at the following illustration and use it to complete the code exercise.

We have a scene here with an intrepid rancher squaring off against four, um, let's call them wild boars. The following conditions apply:

The rancher has six bullets in his gun

There are four charging wild boars

Each boar is charging at a speed of 2.5 feet per second (ok, not a very aggressive charge but still, wild boars!!)

It takes one bullet to kill one boar

The red line is the rancher’s property border line. He is exactly 10 feet behind it. If any boars reach the line, and he’s out of bullets he’ll have to run for his life.

The rancher’s running speed is only 2.0 feet per second.

Answer the following questions. Use any variables and operators you need and print your answers to the console.

If the rancher is able to kill each boar before it reaches the border line (with one shot each), how many bullets will he have left?

How would you use the modulo operator as an alternative way to find the answer to the previous question?

If the rancher goes all “

*scarface”*on the boars and decides to charge the border line as he shoots, how many seconds will it take him to reach the border line (assuming the boars don’t get him before that)?If a boar successfully crosses the border line, and the unlucky rancher runs out of bullets, how many seconds will it take the boar to reach the rancher’s current position?

If the rancher turns around and runs, how far could he get in the same amount of time it takes the boar to cover 10 feet?

In this chase scenario, how long would it take the boar to catch the rancher?

How far away from the border would both boar and rancher be when the boar finally catches him?

We’ll cover C-sharp data collections next.

**An Artist's Guide to C# in Unity3D**