Lesson 7: Scope and Access Modifiers

From this lesson moving forward, we’re going to be speaking of C# scripting in a way that more relates to its functions within Unity. Scope and Access Modifiers is a fantastic place to begin.

Before we begin, let’s set our state of mind to the following:
When you are writing scripts for your game, whether it be behavior scripts for character movement, physics, or shooting a gun, we can safely say that it will be necessary for these systems to interact.

What is a Scope?

The scope of a variable is the area in the code in which the variable can be used in. A variable is local to the place in code that it can be used. Code blocks are generally the area that defines where the code can be used, and they are denoted by braces/brackets {}.

Examples of Scope:

Continue reading “Lesson 7: Scope and Access Modifiers”

Lesson 6: Loops

Like Variables, Functions, and Conditional statements mentioned in our previous lesson, it’s best to get familiar with programming Loops — boy-oh-boy will you need loops in your game developing future.

What are Loop statements?
Simply put, Loop statements are ways for you to execute a block of code {} multiple times. If you need something repeated a specific number of times — or an infinite amount of times, it can be written in the code by you!

Why would I use Loops in Game Development?

Please allow me to speak very loosely here. You would use Loops in Game Development if you wanted to repeat an action/behavior multiple times. Maybe you want the animation of a cloud persistently moving left through the x-axis, in which case you create an infinite loop to make it so. Or maybe you want to loop the act of a machine gun shooting until it runs out of ammo, in which case you loop the action until a condition is met (ammo == 0).

Please Note: In my experience, once a condition is met within a loop, the block of code no longer get’s repeated, and the loop is terminated.

Continue reading “Lesson 6: Loops”

Lesson 5: IF Statements

IF Statements! The bread and butter of Unity. The piece de-resistance. Where all the magic happens. Well, some of the magic. Let’s dabble a little on what IF statements are, and how they get used within Unity.

An IF statement is a part of a family of statements that we call conditional statements. This means that you use it to have your code make a decision based on a defined condition. Let’s use a video game example: You love collecting coins in Super Mario Bros. Even better is the fact that you get one extra life by collecting coins, but only IF you reach a total of 100 coins:

private int coins = 0;
private int life = 1;

if (coins == 100)
   {
    life +1; coins = 0
   }

Got it yet?

Continue reading “Lesson 5: IF Statements”

Lesson 4: C# vs. JS

Notice: From this point on I will be referring to the language C Sharp as C# and the language JavaScript as JS.

Why is this topic even important? Well, it is and it isn’t. It’s important because Unity3D currently supports C#, JS (and boo… more on that later…or never, actually.. yeah never), so it’s healthy to understand the very basics in regards to the differences between these two languages. It isn’t important because if you are a beginner like me, the most important thing for you to do is to grab a language and learn, learn, learn!

REGARDLESS, Let’s talk about some basic differences in the syntax and behavior of C# vs. JS.

Classes:

C#:

In C#, scripts are made up of one or more class/classes. You would write your script within this defined class.

public class ExampleSyntax : MonoBehaviour
{  //<---- Notice that all the code is written inside of the class.
    int myInt = 5;

    int MyFunction (int number)
    {
        int ret = myInt * number;
        return ret;
    }
} //<---- Notice that all the code is written inside of the class.

Continue reading “Lesson 4: C# vs. JS”

Lesson 3: Conventions & Syntax

You’ll notice a similar theme running across my blog posts of these Unity3D lessons: I try my best to demystify programming terms in a way that I find human and digestible. My approach will be no different in this post, as we speak of C#’s Conventions & Syntax.

The best way I’ve found to think of Conventions/Syntax is to relate them to real life language rules (English, Spanish, Japanese, etc.). These language all accomplish the same thing: communication from one human to another. For example, a person can ask “Where is the nearest bathroom?” in both English and Spanish, with no clear difference on the question asked besides how it sounds and how it’s said in those languages. We can state that every spoken language has a set a rules and structures in place that dictate how to properly speak it.

C# is no different (In a manner of speaking). Like a spoken language, it also has a structure that needs to be adhered to in order for you to use it properly. This structure is called a Syntax.

Syntax: simply refers to the structure of the language, and some conventions are essential to learning to read and write code.

Continue reading “Lesson 3: Conventions & Syntax”