Lesson 11 - Variables and Operations Continued

Tutorial Series: Introduction to Unity with C# Series

Previous Article  |  Next Article


Transcript

You can perform the calculation and store the result all in the same line as well. You can go like this: 2 + 2 goes into x; the result of 2 + 2 goes into variable x. Then, just input x as the argument for Debug.Log(). Again, just to show there is nothing up my sleeve or I am wrong, which is distinctly possible, there you go, 4 just as we saw before. We call a variable a variable because the information it stores is variable.

Let's demonstrate this. I don't think we have to comment this out because it is... We will just write in some new stuff. So, int x = 2, x = 5, x = 22. The question is, what value does x hold? Well, execution occurs in order, so it holds 2, then on the next line it holds 5, then finally it holds 22. Let's actually output this to see what we get when we put in input x for Debug.Log(). What do you think it is going to be? 22. Were you expecting that? If you were expecting that, it is probably because you realize that when this method is called, which again, it is called in the Start() method which happens when you first run your game, it is executing this entire code block in sequence, the MyMethod() code instruction block. When that executes in full, we are only outputting the value that is stored in x right after it is being assigned 22. In memory, we are assigning 2 to the x variable, and we are assigning 5, and then we are assigning 22; one right after the other, almost immediately. There is no real space between those assignments. It is happening so fast. It then outputs to debug log only as 22. It no longer holds those previous values. You will also notice that we don't have to declare the type more than once. We only declare it once. X is an int. It doesn't change. All we have changed is the assignment.

Let's look at few other mathematical operations that are common; that you are used to seeing, and you will be used to using in Unity. Let's comment that out, and do this, actually keep this. We will declare it, not assign it. Actually, we will assign it; assign it 2, and then we will do this: x times x. x divided by x. There you go. Let's run that and see what we get. There we are, 4 is output first, then 1, and then 0. Is that what we expect? Well, x = 2; x times x, 2 times 2 is equal to 4, so we first get 4. Then, that is divided by, so 2 divided by 2 is 1, and 2 take away 2 is 0. This makes sense. You get a little sneak preview on the code reuse we get from calling methods multiple times. We are using that same instruction set one after the other, not having to redefine a totally different instruction set. That is just a quick sneak preview on some of the values of using methods, and being able to reuse a code, but that not really what we are interested in right now.

That is multiplication, division, subtraction, and we also saw addition previously. You maybe wonder about mathematical order of operation. What occurs first, multiplication, division, subtraction, addition? The order in which these things occur alter the results. It is the same as in mathematics, multiplication and division before subtraction or addition. To be explicit, you could create your own precedence, and sometimes it is necessary. Often, I prefer to wrap up my operations in parenthesis, even when it is obvious that the correct operation will occur; multiplication will occur before addition. We can do this here; we will comment this out. I don't know let's just give this... For whatever reason, I want to change that to 5. We will write in x*x. We want to do that first, then minus x. In this case I don't have to wrap it up in parenthesis, because multiplication will always occur before subtraction.

The result will be same even without parenthesis, but sometimes when you are coding, adding those parenthesis, it could either make your code more readable or more ugly depending on your opinion on such matters. Here now when we run that...Sorry, I forgot. That was silly of me. I didn't put it in the Debug.Log(). I assigned that calculation to x, but I didn't put it in the Debug.Log(). There we go. Now we can actually see it, which is useful. The result of that calculation is 20. So, 5 times 5 is 25, minus 5 is 20. It doesn't matter if we use those parenthesis or not. If I run this now, yep still get 20. It made it maybe look a little bit cleaner. What if you wanted to do the subtraction first? That will render a very different result. 0, right? Because 5 take away 5 is 0, and now 5 times 0 is of course going to be 0. That is how you can use the parenthesis as creating your own order of operation.

You can use the same principle throughout your code. It doesn't just have to be mathematical operations. You will see them in conditionals, and "if/then" statements, and so on when we work with those later. This code might look a little bit confusing. You might be wondering, “What is the value of "x in this calculation at any given time?” X is the value, in the calculation portion, the right part of the, on the right side of the equals sign. It is the value before it is assigned back to x. We know that the value is 5, then this calculation performs given that value. Then, at the end of that calculation, it gets assigned back into x; x then becomes that value. You can often, and I often look at variables from the right to the left. I kind of read them from the right to the left. I read their value or whatever sort of operation is being performed on the right side of the equals sign, and then as I said earlier, it goes into, sometimes I have heard people refer to it not as the equals sign, but as the ‘goes into operator.’ It goes into that container, x in this case.

That is the basics of working with ints. You will use them whenever you need simply a whole number, and nothing more. You might recall we used ints for counters in our prototype project. Ints are great for basic counters. You will often be using them just for that. Sometimes you want to have a fraction. You want to have, in other words, a number with a decimal place. To represent a numerical value with a decimal place we need a different type altogether. We will actually be using the float type predominantly, probably exclusively, in our coding with Unity, to represent decimal places. We will go into that in the next video. See you there.


Related Articles in this Tutorial:

Lesson 1 - Who This Course is For

Lesson 2 - What to Expect from this Course

Lesson 3 - Installation and Getting Started

Lesson 4 - Starting the First Project

Lesson 5 - Prototype Workflow

Lesson 6 - Basic Code Review

Lesson 7 - Game Loop Primer

Lesson 8 - Prototyping Continued

Lesson 9 - C# Fundamentals and Hello World

Lesson 10 - Variables and Operations

Lesson 11 - Variables and Operations Continued

Lesson 12 - Floats, Bools and Casting

Lesson 13 - If Statement Conditionals

Lesson 14 - If Statements Continued

Lesson 15 - Complex Evaluations and States

Lesson 16 - Code Syntax vs. Style

Lesson 17 - Variable Scope

Lesson 18 - Object-Oriented Programming Intro

Lesson 19 - OOP, Access Modifiers, Instantiation

Lesson 20 - Object Containment and Method Returns

Lesson 21 - "Has-A" Object Containment

Lesson 22 - "Is-A" Inheritance Containment

Lesson 23 - Static Fields and Methods

Lesson 24 - Method Inputs and Returns

Lesson 25 - Reference vs. Value Types

Lesson 26 - Introduction to Polymorphism

Lesson 27 - Navigating the Unity API

Lesson 28 - Applying What You've Learned and Refactoring

Lesson 29 - Constructors, Local Variables in the Update Method

Lesson 30 - Collecting Collectibles, Items and Powerups

Lesson 31 - Spawning and Managing Prefab Powerups

Lesson 32 - Implementing Powerup State Logic

Lesson 33 - Displaying Text, OnGUI, Method Overloading

Lesson 34 - Referencing Instantiated GameObjects, Parenting

Lesson 35 - Understanding the Lerp Method

Lesson 36 - Creating Pseudo Animations in Code

Lesson 37 - Understanding Generic Classes and Methods

Lesson 38 - Animations Using SpriteSheets and Animator

Lesson 39 - Working with Arrays and Loops

Lesson 40 - Debugging Unity Projects with Visual Studio

Lesson 41 - Camera Movement and LateUpdate

Lesson 42 - Playing Audio Clips

Lesson 43 - Routing Audio, Mixers and Effects

Lesson 44 - Adding Scoring Mechanics and Enhancements

Lesson 45 - Scene Loading and Game Over Manager

Lesson 46 - Understanding Properties

Lesson 47 - Controller Mapping and Input Manager

Lesson 48 - Understanding Enums

Lesson 49 - Dealing with Null References

Lesson 50 - Handling Variable Framerates with time.DeltaTime

Lesson 51 - Preparing the Project for Final Build

Lesson 52 - Final Build and Project Settings

Lesson 53 - Introduction to the Unity Physics Engine

Lesson 54 - Understanding FixedUpdate vs. Update

Lesson 55 - Movement Using Physics

Lesson 56 - Attack Script and Collision Events with OnCollisionEnter2D

Lesson 57 - Projectiles and Stomping Attack

Lesson 58 - Parallax Background and Scrolling Camera

Lesson 59 - Infinitely Tiling Background Sprites

Lesson 60 - OOP Enemy Classes

Lesson 61 - OOP Enemy Classes Continued

Lesson 62 - Trigger Colliders and Causing Damage

Lesson 63 - Multi-Dimensional Arrays and Procedural Platforms

Lesson 64 - Finishing Touches

Lesson 65 - Series Wrap


Comments

Please login or register to add a comment