Please share...

  • Video courses

    All game programming videos just $19 - For a very limited time these three videos and thousands more are up to 90% off!

    Just enter coupon code NEWU19 in cart/checkout

C# for Beginners: Learn Fundamentals by Coding

Highly acclaimed course: Over 13,000 students

C# Intermediate: Classes, Interfaces, and OOP

In-depth, classes, interfaces, and OOP.

Learn To Code By Making a 2D Platformer in Unity

Beginning Unity & C# with a real game

Having just learnt what C# game variables are and that they store values that represent the data in our games, it is probably obvious that these values held by our variables will need to be changed as the game is played. For this we need C# operators. Operators in C# are just like mathematical operators we used in school. Many of them are the same symbol. Let’s start by looking at a list of some of the most useful C# operators.

About this tutorial

Skill level 1
Time to read: 10 minutes

New Concepts:

  1. C# operator
  2. Variable assignment
  3. Incrementing variables
  4. Decrementing variables

Projects related to or that demonstrate these concepts

C# operators

Here is a list of some of the most useful C# operators that allow us to do things with our game’s variables. The assignment operator which we have already seen =. This makes the variable to the left of the operator the same as the value to the right. For example look at other ways we might use the C# assignment operator:

// Player gets a new high score
hiScore = score;

or

// Set the score to 100
score = 100;

The addition operator +. This adds the values on either side of the operator. It is usually used in conjunction with the assignment operator, such as:

// Add to the score when an alien is shot
score = aliensShot + wavesCleared;

or this code:

// Add 100 to whatever the score currently is
score = score + 100;

Notice that it is perfectly acceptable to use the same variable on both sides of an operator.
The subtraction operator -. This subtracts the value on the right side of the operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps like in this C# code sample:

// Uhh, I'm dead
lives = lives - 1;

or this:

// How many aliens left at end of game
aliensRemaining = aliensRemaining - numberAliensDestroyed;

The division operator /. This divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this C# sample:

// Make the remaining hit points lower based on the value in swordLevel
hitPoints = hitPoints / swordLevel;

or

// Give player something back for recycling a block
recycledValueOfBlock = originalValue / .9;

Obviously in the previous example the variable recycledValueOfBlock will need to be of type float to be multiplied by the value .9.
The multiplication operator *. Perhaps unsurprisingly, multiplies variables and numbers, such as:

// answer is equal to 100 - of course
answer = 10 * 10;

or

// biggerAnswer = 1000 - duh
biggerAnswer = 10 * 10 * 10;

Now we can look at incrementing and decrementing.

Incrementing and decrementing

The increment operator ++: This is a really neat way to add 1 to the value of one of our game’s variables.

This C# code:

// Add one to myVariable
myVariable = myVariable + 1;

is the same as this:

// Much neater and quicker
myVariable ++;

The decrement operator --.This is a way to subtract 1 from something(you probably guessed).

playerHealth = playerHealth -1;

is the same as:

playerHealth --;

What next

You will get a chance to practice what you have learnt in this and the previous tutorial about game variables when you try the Unity mini-project Unity & C# part 1. I would recommend doing the next C# tutorial first however. In the next tutorial, Conditions and decisions, we will try using some declarations, assignments and operators. When we bundle these elements together into some meaningful syntax, we call it an expression.