# 7 steps to find and design your next game idea

As you build-up your game development skills, you may often be on the look-out for your next game idea; often, this happens to be a difficult process; you may feel stuck first, wondering, how you will be able to find an idea that is both original and challenging for the players; so, in this post, I will cover a few principles that you can start to use to draft and start to design the key mechanics and goals for your game. Continue reading

# Creating a Timer and Pausing The Game in Unity

In most of the games that you will create, you will, more than likely, include the concept of time. This may mean a timer indicating how much time remains before the player loses, or simply, the ability to pause the game.

Implementing a Timer

So first, let’s look at the following snippet example that implements a timer:

float time;

void Update()

{

time += Time.deltaTime;

float seconds, minutes, hours;

seconds = time % 60;

minutes = time / 60;

hours = time / 3600;

print ((int)hours + “:” + (int)minutes + “:” + (int)seconds);

}

In the previous code:

• We use the Update
• We calculate the time since the game started and save it in the variable called time.
• We then calculate the current seconds, minutes, and hours based on the variable time.

If you were to create a countdown, the principle would remain the same, except that the variable time would be initialized to a value that is greater than zero and then decreased every seconds until it reaches zero.

Pausing the Game

In Unity, it is very simple to pause or resume your game, using the following code:

Time.timeScale = 0;//time is paused

Time.timeScale = 1;//time is back to normal

So, you could for example, create two buttons and functions: one button (and its associated function) for pausing the game as well as one button (and its associated function) for resuming the game. You could then call one of these functions based on the button that the player has pressed.

# Communicating between C# Scripts in Unity

When creating your game in Unity, most of your C# scripts will generally be linked to an object. Because these scripts include different types of information, you may sometimes need these scripts to communicate between each other.

# Using attributes and modifying the Inspector

As you create your games, there will be times when you will want to control some parameters at run-time to perform tests. For example: you may want to be able to modify a variable inside the Inspector while the game is running by typing its value or by using a slider to make this easier. In both cases, you will need to modify the Inspector and the way data is presented within.

Thankfully, Unity makes it possible for you to perform these changes from your own C# script. The first thing we could do, is to make our variable public, as all public variables are available and modifiable in the Inspector.

# Creating maintainable and reusable code

When you are coding your game, the size and structure of your code can rapidly become overwhelming over time, unless you have, from the beginning, set a defined strategy that you will follow to ensure that your code will grow in a way that is manageable. To achieve this goal, there are several ways that you can follow including a component-based structure.

# Flash Sale – December 2017

Hey 🙂

To help you kick start 2018 in style, I am offering 70% discount on all my books until midnight this Friday.

# Collision Detection in Unity Explained (i.e., Colliders, Triggers and Ray-casting)

In your games, you will often need to detect collisions between objects. This can be related to NPCs or objects that you will collect or interact with. For this purpose, you can use (and monitor) colliders. However, the way you manage collisions, and the methods to be called in this case, will highly depend on the type of colliders used (for example, colliders or triggers) as well as the object from which you want to detect the collision (for example, simple objects or character controllers).

This being said, you may want to be able to detect the presence of objects located far away from the player or the NPC. In this particular case, ray-casting may make more sense than collision or trigger detection.

So this tutorial will explain in great details how you can detect objects using a wide range of techniques such as colliders, triggers and ray-casting.

# 5 Common Coding Errors in C# and Unity and how to Solve them

As you will start your journey through C# coding, you may sometimes find it difficult to interpret the errors produced by Unity in the console. However, after some practice, you will manage to recognize them, to understand (and also avoid) them, and to fix them accordingly. The next list identifies the errors that my students often come across when they start coding in C#.

When an error occurs, Unity usually provides you with enough information to check the location of this error in your code, so that you can fix it. While many are relatively obvious to spot, some others are trickier to find. In the following paragraphs, I have listed some of the most common errors that you may come across as you start with C#. The trick is to recognize the error message so that you can understand what Unity is trying to tell you.

Again, this is part of the learning process, and you WILL make these mistakes, but as you recognize these errors, you will learn to understand them (and avoid them too :-)).

Again, Unity is trying to help you by communicating, to the best that it can, where the issue is with your code; so by understanding the error messages, we can get to fix these bugs easily. To make it easier to fix errors, Unity usually provides the following information when an error occurs:

• The name of the script where the error was found.
• The location of the error (i.e., row and column).
• A description of the error.

So, if Unity was to generate the following error message…

“Assets/Scripts/MyFirstScript.cs (23,34) BCE0085: Unknown identifier: ‘localVariable’”

…it is telling us that an error has occurred in the script called MyFirstScript, at the line 23, and around the 34th character (or the 34th column) on this line. In this particular message, it is telling us that it can’t recognize the variable localVariable.

So, you may come across the following errors; this list is also available in the resource pack as a pdf file, so that you can print it and keep it close by:

1. “;” expected: This error could mean that you have forgotten to add a semi-colon at the end of a statement. To fix this error, just go to the line mentioned in the error message and ensure that you add a semi-colon at the end of the statement.
2. Unknown identifier: This error could mean that Unity does not know the variable that you are mentioning. It can be due to at least three reasons: (1) the variable has not been declared yet, (2) the variable has been declared but outside the scope of the method (e.g., declared locally in a different function), or (3) the name of the variable that you are using is incorrect (i.e., spelling or case). Remember, the names of all variables and functions are case-sensitive; so by just using an incorrect case, Unity will assume that you refer to another variable.
3. The best method overload for function … is not compatible: This error is probably due to the fact that you are trying to call a function and to pass a list of parameters (which means the number and the types of parameters) that is not compatible with what the function is expecting. For example, the method mySecondMethod, described in the next code snippet, is expecting a String value for its parameter; so, if you pass an integer value instead, an error will be generated.
```void mySecondFunction(string name)

{

print (“Hello, your name is” +name);

}

mySecondFunction(“John”);//this is correct

mySecondFunction(10);//this will trigger an error

```

1. Expecting } found …: This error is due to the fact that you may have forgotten to either close or open curly brackets for conditional statements or functions, for example. To avoid this issue, there is a trick (or best practice) that you can use: you can ensure that you indent your code so that corresponding opening and closing brackets are at the same level. In the next example, you can see that the brackets corresponding to the start and the end of the method testBrackets are indented at the same level, and so are the brackets for each of the conditional statements within this function. By indenting your code, using several spaces or tabulation, you can make sure that your code is clear and that missing curly brackets are easily spotted.
```Void testBrackets()

{

if (myVar == 2)

{

print ("Hello World”);

myVar = 4;

}

else

{

}

}
```

1. Sometimes, although the syntax of your code is correct and does not yield any error in the Console window, it looks like nothing is happening; in other words, it looks like the code, and especially the methods that you have created do not work: This is bound to happen as you create your first scripts. It can be quite frustrating (and I have been there :-)) because, in this case, Unity will not let you know where the error is. However, there is a succession of checks that you can perform to ensure that this does not happen; so you could check the following:

• The script that you have written has been saved.
• The script contains no errors.
• The script is attached to an object.
• If the script is indeed attached to an object and you are using a built-in method that depends on the type of object it is attached to, make sure that the script is linked to the correct object. For example, if your script is using the built-in method OnControllerColliderHit, which is used to detect collision between the FPSController and other objects, but you don’t drag and drop the script on the FPSController object, the method OnControllerColliderHit will not be called if you collide with an object.
• If the script is indeed attached to the right object and is using a built-in method such as Start, or Update, make sure that these functions are spelt properly (i.e., exact spelling and case). For example, for the method Update, the system will call the method Update every frame, and no other function. So if you write a method spelt update, the system will look for the Update function instead, and since it has not been defined (or overwritten), nothing will happen, unless you specifically call this function from your code. The same would happen for the method Start. In both cases, the system will assume that you have created two new functions update and start.