Introduction to Coding

James Doyle
A free video tutorial from James Doyle
Teaching The Basics of Making Games
4.6 instructor rating • 12 courses • 41,438 students

Learn more from the full course

Learn to Create & Publish Games for Mobile

Mobile development made easy. Learn C# using Unity and create & publish your own mobile game

11:33:39 of on-demand video • Updated April 2018

  • Publish a full game to the Google Play Store
  • Understand how to earn revenue using advertising in their games
  • Learn C#, a modern versatile programming language
  • Understand the capabilities of mobile development in Unity
  • Develop strong and transferrable problem solving skills
  • Gain an understanding of the game development process
English [Auto] So if you're not familiar with coding in unity or coding in general we're going to take a quick little look at some of the basic elements that make up a script within unity. So the first thing we want to do is just create a new script. We'll just create a new default when it doesn't really matter too much what this one is going to create a new C-Sharp script. So we click in our product area race C-Sharp script like so and we just call this we just call this test. Now when we're creating a script generally speaking am to a certain format you want to use. So we don't just call this test we'll call this test script just to use as an example. And the format you always use when you're creating a script is every word within your script name you start with a capital letter and an easy way to break it apart into little pieces what you can never ever do when creating a script is put a space in between your words if you put a space in between your words like that. Is it basically use you won't understand what a script works and explain why that happens now in a second so I'm just going to take that space. I'm going to hit enter here. So that creates the script and by default. Now that I have a select over here you can see it shows appear in our inspector and by default it fills in a whole bunch of information into unity that unity provides by default basically. Right here you can see just public class and test script which is the name of the script we have and unity will actually put this in here. Boss if you were if you were to type in for example if I go ahead and create another script and this time I called test space script like this you can see it on non-secret And over here. But it takes the Space X because you can't have a space in your script name. So now basically what happens is when he goes to load the scripts it's looking for a script that has a space in the middle of it. But over here that doesn't exist. So basically by put the space in it just becomes all kind of broken and weird. You can see down here we've got an error now of a sudden which is obviously not very good for us. I'm going to delete that script again and we're just going to open up our test script here. Now I'm open up in Visual Studio you might open up and Manoa develop am which is built into you and you but if it doesn't matter what you open up and as long as we can type in some letters and numbers that's all that matters. Let us open up here. OK. And I want to open with the we have our scripts open here. So let's just go through the basics are part of a unity script. So by default at the start we have these things up at the top. We've got using system connections using System collections generic and using use the engine. So these are just three basic default things that allow us to work within C-Sharp and work in unity. So these are systems that allows to access C-Sharp systems that are built into developing in general on Windows and then we have using unity engine which allows us to use utilities specific components. You don't need to worry too much about us. Basically all the saying is that this is how we're able to use specific unity things that are other things as true as the Course will add into this area on different scripts. For example if you want to use UI or anything and any any coding you want to do with the UI we'll have using unity engine does UI is a is a good example of one. But we don't need to worry about that for now we'll deal with that as we come to. So next we have public class test script. So as we said before test script is just the name of your script is automatically filled in here and every script you add in unity generally speaking. You need to have a class that matches the script name. We have manone behavior which means that a is an element that you and you will understand knows how to process as a kind of standard object we're in unity you can do different objects here and you can inherent in Haras different classes and things like that. But again that's more advanced stuff that you don't need to worry about when you're starting off. All you need to remember is that every script will start with a class test script. Colin Manoah behavior and then we've got a bracket here that opens and don't if you highlight there like this you can see the bottom brackets also gets highlighted and it's a curly bracket closes. So basically what it says is all all information about this public class test script is within these two brackets. So everything inside here is stuff that will be run by unity when it's trying to run the test script. So the other things we've gotten here are voyde start and voice updates and you can see with these little lines here. We've got this green tech So when you put two lines like this it means it's a comment which means that there's little bit here won't be read by unity. So when you want to leave a message to yourself basically you just type slash slash. Here's a little reminder or something like that so last week you'll put them in to remind yourself oh this is this is what this little section of code does. And as it says here we've got use this for initialization before void start and void starts is basically something that happens as soon as this script runs. So as soon as you're playing your game and unity as soon as an object in the world with descript attached to it and appears it'll run this little stack function. Otherwise we have a void of this. And as you can see here is as it is called once per frame. So basically when we wanted to run regular code within unity we put it inside of these curly brackets in the update loop which means every single frame of the game happening and nothing will happen. So say for example if we wanted to AM updates something Wes and you numbers are we are. I'd say 5 to something every frame we would added in here. So we'll go five 10 15 20 25 et cetera very very quickly. But that's basically the main area that we'll be working within with in unity. So those are two basic functions that are built in by default to unity and one other main thing that we need to remember and understand is the idea of variables and variables are at a basic level just objects are values that we want to keep track of and where we will we want to create variables in general appear before our start function. We can just type in here. For example I'm going to create an invariable So an invariable is just a number value and any any number value is a whole number. So one two three four twenty seven 156 whatever like that was that doesn't have any decimal places. It's an int number. So we're gonna create an int coal's say we'll just call it test int like so. And we don't need to give us any value. So we were creating this in cold test it. Now if we were to save this script this is old. This is the only change we've made to it. We could save this and we go back into unity. Here you see down in the bottom corner here a second ago there was a little rotating little spinning image that shows when you save a script when you have it open. When you save it then and go back into unity it has to compile that script. So the unit was able to run whatever whatever data and changes you just made. So you have to wait for that little spinny thing to stop and then you can go and do whatever it is you want to do. So what we're going to do is just going to create a new empty object doesn't really matter what it's called I'm going to grab this script and drag it onto there and put it on here and you can see here now we have our test script attached. You'll also notice that our test script now has a space here and the component because when you capitalize letters like this in the name of the script you actually automatically knows that. Oh when we showed us in as a component we can just put a space before that to make it easier for everybody to read. Now you'll see in here we created an int value as a number but we don't see it here in Unity. It's not like if we go say to our camera over here you can see these are these are values these are numbers here and they're essentially running off your own script for example size here is done in value because it's got a decimal places. It's called a flow value and plus it shows up here. So why doesn't our number show up on our test script. Well that's because there's one important thing you need to remember when creating any kind of variables or anything like this. If you wanted to show up in the editor and use a unique type public before us like that because there's two there's two different ways of showing numbers we can have a public event or we could have a private. So if you don't type it or one of these before is it by default we'll go to saying OK this is a private want it doesn't want to be shown. So what we can make sure we do is type public interest test and things like that. Now if we save us and go back in here and see test it magically appears. So we could set this to be whatever value wanted like that. We don't want to wait too much but that this was an example of how we can show a variable within unity and then of course we have this variable so we could do things. For example like I said in the update here we could say test it equals test it's plus five. So every update it would update dash test in value so we could just as a quick example see this in unity. Just save this go back in here. See I tested it is zero. So if I was to go ahead now and hit the play button up here you can see we look at our test and every single frame is getting five out of the whole time. So that number is very very quickly rising up very very high. Stop this so you can see at a basic level these are the main things that these variables appear though won't just be in values. Of course we can have strings which are collections of letters and characters we can have whole objects we can have and our player capsule that we created or tiles that we created will store data about their positions within the world and things like that we will introduce all those stuff as we go. But for now it's important to understand that we can have variables up the top appear and then we didn't update our starter or any other functions that we created. We will control what happens with those variables. OK so with that in mind obviously we don't really want this script to be something we're going to use within Unity's it's going to close this script. We're going go back in here and going to delete the objects that we just created on the script because we want to put our scripts in an actual folder not just in the same scenes folder we have here. So we want to have a specific place to put our scripts and that's what we're going to take a look at doing in the next episode when we start looking at how we can get our world and our player moving around.