Hello Ace This is RetroTK2 and today you’ll learn how to declare variables in C# You’ll also learn some of the basic functionality of the ‘=’ operator If you haven’t already Ace, I’d advise you to download the ‘Channel-Files’ that accompany this video The card should be on your screen now… So let’s get started… Well, what is a variable Ace? According to MSDN: “A variable represents a numeric or string value or an object of a class.” “The value that the variable stores may change, but the name stays the same.” “A variable is one type of field” “In C#, variables are declared with a specific data type and a label” I really didn’t want to get onto the topic of variables until a little later in the series Ace, but I felt I had to in order to make the next couple of tutorials easier I’ll be glossing over quite a lot of important information here, since I want just enough to get us through the next tutorial Open up Unity and create a basic C# script As always Ace, make sure the script has a different name than any other class in your project We’ll be declaring ‘local’ variables in our ‘Start’ method This means that we won’t be able to access these variables outside our ‘Start’ method’s body Again, quickly glossing over this stuff Ace since I really want to explain variable ‘scoping’ in a future video For now, simply know that anything we declare in the method body of the ‘Start’ method will only be used inside that method Ok, let’s declare a basic variable Here I’m going to declare an ‘int’ called “x” You’ll notice that in order to declare a variable, I have to specify the type I want it to be… In this case an ‘int’ And give it a label… That’s a name to you and me Ace So now, if we try to log our variable out to the console, you’ll see that we get an error: “Local variable ‘x’ might not be initialized before accessing” Unfortunately, we need to give our variable a value before we can use it To do that we must use the ‘assignment’ operator… That’s the ‘=’ sign Ace… And give it value Let’s give it a value ‘0’ Now if I save my script… Head back into Unity… Drag our script onto one of the GameObject so that it gets called… and run our game… You’ll see that ‘0’ is logged to the console All very basic and hopefully pretty easy at this point Ace Let’s try and assign ‘x’ to a ‘float’ number Simply suffix your zero with an ‘f’, and you too can witness the big error underlining our ‘x’ variable “Cannot convert source type ‘float’ to target type ‘int’” Put simply Ace, a ‘float’ literal is not an ‘int’ and so we need to change the variable’s type before we can assign it to a ‘float’ value So if I change ‘int’ to ‘float’, you’ll see that the error disappears If I save the script, head back into Unity… And re-run our game… You’ll see that ‘0’ is logged once again Using the basic operators we looked at in the last video… The card is on your screen now Ace We can do some basic mathematics with our ‘x’ variable Ok, I want to add ‘2’ to my current ‘x’ variable’s value To do this I type out ‘x=x + 2’ This means ‘x’ will be assigned the result from the equation ‘x + 2’, which in this case will be 2, since ‘0 + 2=2’ With me so far Ace? This should hopefully be pretty easy for you Now, we’ll log out the result just to prove it Save the script… Head back into Unity… and re-run the game… and you’ll see that ‘2’ is printed Exactly what we expected Ace Let’s copy what we have just done and place it below We’ll be doing the same mathematical operation to the ‘x’ variable, but since the variable’s value has changed to ‘2’, we should expect to see ‘4’ logged to the console Save the script… Head back into Unity… And re-run the game… And you’ll see that ‘2’ is logged and so is ‘4’ Very basic algebra here Ace, but this is the fundamentals of coding Feel free to experiment with this for now Ace, I don’t want to move onto anything further until I’ve discussed more of the operators with you This is super basic variable assignment in C# And that’s it… Rate, Comment and Subscribe Ace, thank you so much for 300+ subs You can email me at [email protected] with any suggestions Thank you for watching Ace, and I’ll see you in the next video