Unity C# Scripting Essentials for Game Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to charger games my name is Raja and in this video we're gonna learn about essential C shop scripting concepts with unity buy a lot of different examples so if you want to learn about the basics of c-sharp and absolute basics of c-sharp then you can check out my other videos or other courses where I talk about that in details but this one is gonna be practical full of examples and projects so all the links to my different videos and courses and in the description of this video so you can go ahead and check them out so with that being said let's get started so first of all I have a sprite here this tanks right which I'm gonna use to demonstrate different things you can use any other image that you want so I'm gonna go ahead and drag and drop it in our hierarchy and I want to go to our main camera select the background and change its color to something like this so that it looks pretty good and not the default color which I don't really like so I think I'm gonna keep it something like this then I'm gonna go ahead and change the size of our main camera a little bit so that our tank becomes bigger so I think I'm gonna make it something like this let's make it three all right so now we're gonna create our very first script and attach it to our tank so I'm gonna go to assets folder create a new folder named scripts and inside the scripts folder right-click create a new C sharp script and I'm gonna name it let's say examples and also you need to remember that I have created a 2d project here so if you have not created a 2d project then you will not be able to import these sprites like this now I'm gonna go ahead and select my tank and drag and drop this switch our script on this tank now I'm going to double click to in the C sharp script so now as you can see our script has opened I'm gonna make the font size a little bit bigger so as you can see here we have the start and update function the start function gets called at the beginning of the game and the update function gets called every frame so every frame our game runs this update function gets called so now for the first thing that we're gonna learn is let's go ahead and learn something simple so first we're gonna learn how to destroy a game object so to destroy any game object we need to simply write destroy which is a function so we need to give a pair of parentheses and inside that way the right game object and then a semicolon so that pretty much means we're going to destroy the game object and which game object we want to destroy we want to destroy the game object with which this script is attached so let me go ahead and save it and if you go back to unity you'll see that our script is attached to this tank so that pretty much means we want to destroy the tank as soon as the game starts so let's see and go ahead and click on play and you will see that as soon as the game starts the tank gets destroyed because we have told it to do so so that's how we destroy the tank so now let's see another example of how we can do something more with this so let's say we don't want to destroy it immediately but we will destroy it after three seconds or five seconds to do that after writing game object we have to give a comma and then we to write the number of seconds after which we want to destroy the game object so in this case let's say I want to destroy it after three seconds so I'm going to write three F so now after starting the game the object will be destroyed after three seconds so make sure to save it go back to unity now let's click on play and count one two three boom and as you can see after three seconds the tank are destroyed I actually counted three very fast so pardon me for that so that's how you destroy objects in different ways so now let's go ahead and learn how we can destroy the same object by clicking on that object okay so now we're going to learn about mouse clicks and how we can detect mouse clicks so in order to detect mouse clicks on any object we'll to use this function void on Mouse down so this void onmousedown function gets called automatically whenever we click on any object all right so whenever this object gets scored that means whenever we click on an object then we want to destroy that object all right so now we're gonna copy this code and paste it here and instead of 3f I'm just gonna simply write this striking object because we want to destroy the object immediately nor after three seconds all right and now we don't want to destroy the game object at the start so I'm gonna write double slash here so that means we are writing it as a comment that means it will not affect our code it will stay here but it will not be affecting our code by any way so now whenever we click our mouse on the object we want to destroy the object so now let's go ahead and click on play and see if it is working at all or not so if I click on play as you can see when I click on it nothing happens and that's because we are clicking on the object but our mouse doesn't know if we are clicking on it or not so in order to detect clicks or collisions we need to add a Collider to our game object all right so let's go ahead and select our tank then go to add component and then go to physics 2d and from here we gonna use this box Collider 2d so if you double click on the tank and zoom in it as you can see here we have a green outline and you can use this edit Collider option to edit the screen outline so this green outline is now the collider of this tank so this is the area in which we can collide and interact and click on the tank alright so now if I go ahead and click on play now I can go ahead and click on it and as you can see as soon as I click on it the tank gets destroyed let me see it one more time three two one boom and as you can see as soon as I click on it the tank gets destroyed so this is how we can destroy the tank by using mouse clicks so this is how we take mouse inputs very easily so now let's go ahead and learn how we can do the same thing but instead of mouse this time we're gonna do inputs from our keyboard so let's learn how we can take inputs from the keyboard and then destroy the object so in order to destroy the object using keyboard inputs we're gonna come to our update function and here we're gonna write if input dot get key down that means if we are pressing any of our keys and then inside this we need to write which key we want to detect let's say in this case we want to say whenever we place the space button on our keyboard we simply want to destroy our game object so here when to write key code dot space and all these are predefined so you have to write exactly as they have been set in unity so you cannot simply make the is small or write some other spelling so it's a good idea to use the auto-completion in this case is to write the perfect thing that unity requires you to write alright so here what we are doing is first of all we are getting input dot gate key down key code dot space and this thing returns true whenever we press the space button on our keyboard so this if statement chicks if we are pressing the space button on our keyboard and if we press the space button our keyboard then what we were to do if we want to destroy the game object all right so here we can I'm gonna write destroy game object so this way whenever we're gonna press the space button on our keyboard we want to destroy the game object all right so now let's go back to unity and of course save the script every time you make some changes so go back to unity click on play and now I'm gonna press the space button and as you can see as soon as I press the space button the tank gets destroyed so let me try it one more time so three two one space and as you can see as soon as I press the space button it gets destroyed so this is how we can take the basic keyboard input in unity alright so now that we have learned how to take keyboard and mouse inputs it's time to move our tank so let's see how we can move our game objects in unity very very easily now in order to move our game objects there are various different ways we can either change its position directly by using some code all we can use the transform the translate function to translate it to a certain position or we can use the UTS built-in physics objects or physics properties and physics functions to move the object so in this case we're going to try to use the physics functions to move the object and if we want any of the physics functions to be used on any game object in unity we need to add something called the rigidbody 2d on that object alright so here we need to add the rigidbody 2d to this tank as well so here I'm gonna come to add component go to physics 2d and add the rigidbody 2d here it is so now that we have added the rigidbody 2d we also need to set the grab scale to zero because if we said to one then the gravity will affect the tank and it will fall down as you can see it falls down so we can set the gravity to zero all right so now that the gravity is zero and we have the rigidbody attached to the stank now we need to move the tank so let's go ahead and open our script and as you can see so first of all what we need to do is we need to get access to the rigidbody that is attached to our tank all right so first of all here we're gonna create a reference so we're gonna write rigidbody 2d R B so this RB will store a reference to the rigidbody 2d that is attached to our game object so in the start function we're gonna say our V equals get component rigidbody 2d so now what it does is this gives us access to the rigidbody 2d that is attached to this game object and stores the reference inside this RB variable so now everything that we want to do with the rigidbody we can do it using the RB all right but it is a good idea and it is a good practice to do this referencing things or getting access things all this in the awake function instead of start function so here we're gonna write void awake and inside awake function I'm gonna go ahead and move this thing so whenever you are getting a reference to some objects or components or whenever you are getting access to new components it's a good idea to use it inside the awake function because awake gets called before started before starting the game so this is how we get access to the rigidbody and now when you do something with this body so what we can do is to move the tank or move the player game object we can change its velocity all right so to change its velocity we can go to start and here we write our we dot velocity equals vector two dot right multiplied 5f so now what we'll do is let's see what it will do so let me go ahead and click on play and as you can see the tank will move to the right with this speed alright so here what we are doing is we are giving the tank a velocity in the right direction and we are multiplying it with 5f that means we are giving it a velocity of five in the right direction now it's a good idea to always not hard-code these values like this and store it as a variable so that we can set it and change it anytime let's say we want to increase the velocity so we don't have to change this every time we have to simply change the value of the variable and this will work fine so what we can do is here we can create a public variable so you can write public float speed so the speed River will de mine how much fast or slow our object will move all right so here instead of writing 5f we're gonna simply write speed so now the speed value will be multiplied with vector three that right that means we are adding the velocity in the right direction and then setting that velocity to our rigidbody alright so now let's go back to unity and select our tank and as you can see if you select the script let me move the script to bottom so you would to bottom like this alright so as you can see here we have the speed value set to zero that means now if I go ahead and run it our tank will not move at all so what we can do is we can give it a speed value let's say - and now you will see our tank will slowly move by two in the right side we can give it a speed value of let's say 10 and now it will move faster and then let's say we give it a speed value of minus 10 well let's say for now - - and if I run it you will see that it is going to move in the opposite direction because we are multiplying the velocity with negative 2 so we are multiplying the velocity with nearly 2 that means it is moving in the opposite direction another thing you can do is instead of multiplying it with negative 2 we can keep it 2 here and in the code instead of writing victor student write we can simply write victor - dot left so now instead of applying the speed on the right side this will apply the speed of the left side so let's go and check if it works so if I click on play you will see now it moves on the left side so here what we had doing is in the start function in the starting of the game we are giving a velocity to our game object so we are giving a fixed velocity to a game object at the starting of our game so our game object keeps moving with the velocity as long as the game runs so this is how we can easily add movement or velocity toward game object using the physics functions ok so now that we have learned how to add velocity to our game object now let's learn how we can move our game object let and write using keyboard inputs so now as you can see here we have learned a way to take keyboard input by using this input that kick it down but there are other efficient ways to take inputs when we are taking continuous input from the keyboard so let's say I want to move the tank to the left as long as I keep pressing the left arrow key the same way I want to make the tank move to the right as long as I press the right arrow key now in order to use these things in order to check whether am pressing for continuous amounts of time unity has created a feature for us and it has set something called an axis so using this axis we can use some built-in keyboard functionalities and use them in our game all right so if I go to here in the unity and go to edit project settings and then I go to input as you can see here we have something called axis and if I just expand it as you can see here we have something called horizontal and for the horizontal axis the negative button is already set to the left arrow key and the positive button is already set to the right arrow key all right so when we are going to use this axis and whenever we are pressing the let arrow key automatically it will set it as its negative button and set the right arrow key as it's positive button so whenever we use this input dirt gate axis horizontal function it will return us a value between 0 and negative 1 or 0 and positive 1 so when we are pressing the left arrow key it will return us a value about 0 to negative 1 and when we are pressing the right arrow key it will return us a value of 0 to positive 1 so using this negative 1 and the positive 1 value we can determine whether we are pressing the left or right arrow key and we can move our tank accordingly now it will become clear as you see it as I code and you do it yourself as well so inside the video studio now what we will do is here inside the update function okay first of all here will create a separate variable so here we'll write float X input so this input will take the value of the left or right arrow keys from our input delegate axis function so inside the update function we're gonna write X input equals input dot gate access and inside there is right horizontal why because just a few seconds ago we have seen that unity has already set up an axis called horizontal for us for which the negative button is left arrow key and the positive button is right arrow key so just by writing horizontal we can automatically get inputs from the left and right arrow keys so when we are pressing the left arrow key the X input will have a value of negative one and whenever we are pressing the right arrow key this X input will have a value of positive one and when we are not pressing anything this X input will automatically return to zero so this way we can easily take inputs from our keyboard now we need to use this keyboard input and move our game object or add velocity to a game object accordingly now in order to do that as you can see here we are adding the velocity using the physics functions in unity so whenever we are using the physics functions it's always a good idea to use the fixed update function instead of update or start so for the physics functionalities we should always use the fixed update function so here we gonna write avoid fixed update so this is the fixed update function now inside the fixed update function as you can see here we have this arbiter velocity I'm just gonna go ahead and copy it and paste it right here and I'm gonna comment this one out so this is not going to use it used at all so instead the fixed update this fixed update is getting called every physics frame so this function will get called continuously as well just like update but update gets called every frame but fixed update gets called every physics frame so whenever you are creating some physics behaviors for your game it's always a good idea to put it inside the fixedupdate so that you get reliable and good results so inside this we are saying our aided velocity equals victor to the left x speed so instead of that what we gonna do is we're gonna say victor to the right and then we got to multiply it with x input and then we're gonna multiply it with our speed all right so here what you're doing is victor right simply means in the right direction and X input gives us a value of either negative 1 positive 1 or 0 so whenever we are pressing the right arrow key this will automatically become 1 so it will automatically move to the right side by this speed and when we are pressing the left arrow key this X input will become negative 1 so that means this together will be a negative value negative 1 and then we're gonna multiply it with our speed all right so then it will move to the left side and when we are not pressing anything the X input value will be 0 that means this whole value will be 0 that means the velocity of our object will be 0 as well so this way by using this simple input control we can move our object left and right so now let's go back to unity and select our tank I'm gonna change the speed - let's keep it - for now and I'm gonna click on play to see if it is working or not and I'm gonna press the left arrow key and then I'm gonna press the right arrow key and as you can see nothing is working and and here nothing is working because you can click here to see the error and as you can see it says input axis horizontal is not set of and that is because I have made a typing mistake you provided exactly as it is written in unities axis otherwise it's not going to work so it's always a good idea to go to edit project settings input and for the horizontal you can just copy the name from here and write it exactly as it is written here so I have pasted it from there to here so now it is written exactly as it is in the unity settings so now I can close it and close this one as well click on play and now as you can see if I press the left and right arrow keys on my keyboard I can move the tank left and right by this to speed I can also change the speed to let's say 5 and then again I can move it with faster speed to left and right direction so this is how we can move a game object left and right using the arrow keys on your keyboard very very easily now another thing you will see that here the valve is 5 but as soon as I stop playing the game the value will return to so whenever you are changing the values while playing the game they will not stay so you have to remember that you make all the changes while you are not playing the game so here I'm going to give the value 5 and it should be fine so this way we have learned how to make the tank move left and right alright so now let's go ahead and add it terrain so that I can so that our tank can walk on it or move on it so I'm gonna go to this sprites folder and I'm going to drag and drop this to cross meet sprite that I have in the scene and I'm gonna move it down below like this I think I want to move it to somewhere like this now I'm gonna select this cross mint not this one this one the cross made change its draw mode to dial and now I'm gonna select this Dule and then i'm gonna go to this craftsman in the scene view now what I can do is I can simply drag it and as you can see it will move because we have changed it to tile settings so all objects will be tiled together like this now we can drag this grass center act here I can move it at the bottom of this and we can match it exactly as it is by using this red tool so we can drag it and move it to match it with this one something like this then we can go ahead and change it to tiled as well so I'm gonna go to this cross Center change its draw mode to tiled and then I can drag it like this and drag it like this on this way as well so now we have this thing now we can press shift and select both of these things together by pressing the shift key on my keyboard then use the move tool and move it a little bit down so that they look realistic something like this now I have to give a Collider to my grass mitt which is on the top so that our tank can collide with it otherwise what will happen is whenever we will put our tank here it will go through it because our ground has no collider so it cannot collide with the tank so I'm going to select the cross mid and go to add component physics 2d box Collider 2d and as you can see here we have this green thing that is our box Collider now I'm gonna click on this edit Collider button and by selecting this point I'm gonna drag it to this side and selecting this point I'm gonna drag it to this side so now our ground has a Collider which is pretty much big now what I can do I can select our tank go to the rigidbody and give the gravity scale back to one and now if I click on play you will see our tank will fall down due to gravity and it will land on this one alright so as you can see now our tanker lands on this one and it is it is going down very slowly because in every physics frame we are changing its velocity by our code not this one so if you see here as you can see in our chord every physics frame or every in every fixed update we are changing its velocity so that is why it's gravity it has become very very slow alright so now we can move the tank to a place something like this and if we want we can simply remove the gravity because we don't need it we are not going to make the tank do something like that so let's go ahead and change the Comiskey go back to zero again so now our tank is zero and we can already move our tank left and right by using arrow keys so let's go ahead and test if it is working still not so I can press the lift and right arrow keys and move the tank left and right and now it looks like it is moving on the ground so now let's try to do something else let's try to rotate our tank to the left side whenever it moves to the left and rotate our tank to the right side whenever it moves to the right now a very easy way to do this is by flipping the sprite of our tank so as you can see here we have this tank and here we have something called a sprite renderer attached to our tank and here we have something called flip so if I click on this flip X as you can see the sprite will rotate and flip to the X direction and if I uncheck it it will rotate again so if we can access this component from our script then we can simply rotate the tank to the left or right direction all right so let's see how we can do that so here we are back inside the unity the visual studio so here we're gonna create a reference to the sprite renderer that we have in our scene for our player or for our tank so here we're gonna write sprite renderer sprite alright so now inside this sprite will store the reference of the sprite renderer component now there's another way to do that we can simply write public here and as you can see before while accessing the rigidbody 2d in the awake function we have used this gate component function and using this function we have got access to the rigidbody 2d now we can do similar thing with our sprite as well so we can simply write sprite equals kit component sprite renderer but here let's learn another way to do it so here as you can see I have made this sprite renderer public now if I go ahead and select the tank here you will see here we have a field called sprite which is waiting for a sprite renderer so in this slide renderer we can either drag this sprite right here so from here as you can see I can simply drag the sprite renderer and drop it here or I can simply use the tank and drag and drop it here as well so it automatically converts it into a sprite renderer reference so this way when we drag and drop this one to here we don't need to manually do it from our code by writing this gate component function so in this case I just showed you this so that you can learn both of these things and use them according to your own wish or whenever you need whichever functionality so now that we have access to this sprite renderer inside this sprite variable inside the update function we will check when our player moves to the left or when the value of our X input is less than 0 that means whenever our player is moving to the left or whenever we are pressing the left arrow key in that case we want to flip it or flip the X correction I mean with the X of the sprite and whenever we are pressing the right arrow key we went to uncheck the flip X property or we want to make the flip X false so let me show you to you here as you can see here we have the tank and here we have the flip so when we are moving to the left we simply want to check this flip X that means we want to make the flip X true and when we are moving to the right we want to make the flip X false as you can see currently it is false all right so here we're gonna say if X input is less than 0 then we're gonna say sprite dot flip x equals true all right then we're gonna say else if X input is greater than 0 then sprite dot flip X because false so now whenever the X input value is less than 0 that means we are pressing the left arrow key we are going to flip the sprite and whenever the X input is greater than 0 that means we are pressing the right arrow key we're gonna make the flip X false so that it returns to its default position so let's go ahead and save it go back to unity and see if it is working or not so I'm gonna click on play and now you'll see whenever I press the left arrow key the tank automatically moves to the left I mean rotates to the left or flip X and as you can see here the flip X becomes true and whenever I move to the right this flip X becomes false and as you can see the tank moves to the right so this way I can make the tank move to the left and right direction and also change its rotation like this by flipping the sprite like this so this is a very useful feature that you can use in a lot of games that he create so now let's try to do something more interesting now we're gonna check when our tank is colliding with another object and then we're going to destroy that object so let's say as you can see inside the sprites folder here we have something like a box so I can drag and drop it in the scene then move it like this so let's say I had the box here and whenever the tank moves and collides with the box I want to destroy the box so that's what we can a good right now now before we can collide with the box we have to add a Collider to the box because if we don't add any Collider then our tank can simply go through so let's see it see if I click on play you can see that our tank and go through the box and nothing really happens so we don't want that we want to go to the box and at that time we want the box to be destroyed to do that first of all I'm gonna select the box go to add component physics 2d box Collider 2d so this way now our box has a Collider attached so now if I click on play you will see that our tank and move and it will collide with the box alright so it's not easy now or not possible now to go through the tank so now I want to destroy the box whenever it collides with the tank so to do that I want to select the box go to this back option and as you can see currently it is unpacked I'm gonna go to this add tag and from here I'm gonna add a new tag I'm going to call it enemy let's say let's give it enemy for now so if you want to add more tags you can simply click on this plus button and add another tag here as well also you can click on this negative button to remove it now again you need to go to this box select the tag and click on enemy so now the enemy tag is attached to our box now we need to write a script or from our code when you write something so that it will check whenever our tank collides with an object which has this enemy attack attached then we have to destroy that object alright so now let's go back to our code and in order to check for collisions unity has a specific function which is called on collision enter to D so here we're gonna write void on collision enter to D and let's auto complete it and here we have this function and either to write this parameter exactly as it is written all right so collision to D you can write it collision or you can write it call so this doesn't matter but you need to write it as a collision to D in the parameter so this is the on collision entered to D function which gets called automatically whenever our game object collides with any other object all right so in this case what we want to do is whenever our game object collides with another object first of all we want to check if that object has an enemy tag attached so to do that we're gonna say if collision that game object dot tag equals enemy and here as well you need to make sure that the spelling of enemy is exactly same as you have written in unity so you can simply go to unity and as you can see here the tag we can go to a head tag and from here I can simply go ahead and I'm not sure if we can't copy the enemy but I can I can go here and look at it and write it exactly as it is written here so I can go back here and as you can see here we have written enemy exactly as it is written there so whenever our game object collides with an object which has the enemy tag attached then we're gonna do what we can do is we're gonna simply destroy and this is tricky so which one we will destroy if we write destroy game object what will happen is the tank will get destroyed so let's see if that is working so now if I click on play move here and as you can see the tank itself gets destroyed so we don't want that we want to destroy the box instead of the tank to do that instead of writing game object here I'm gonna write collision dot game object alright so if we write collision or game object that means it will destroy the game object with which the collision has happened and if we write simply a game object so if you write destroy game object this will means it will destroy the game object with which the script is attached and in this case the script is attached to the tank so it would have destroyed the tank itself but we want to destroy the object with which the tank has collided that's why we are writing collision dot came object all right so now save it go back to unity and click on play and now you will see whenever I go ahead and as you can see the box gets destroyed immediately so let's go ahead and create few more boxes and let's have some fun so I'm gonna press control D on my keyboard or right-click duplicate it and as you can see here we have look at it and I can move it right here I can duplicate it a few more times move it here and there duplicate it here and there and as you can see here we have four different boxes so now I can click on play and our tank and go here destroy this one let's try this one destroy this one and destroy this one as well so this way as you can see our tank has destroyed all the boxes in the scene whenever it collides with them so this way you can destroy the box you can also add some scores so that whenever you destroy the box the score gets incremented and after some time when you destroy all the box here right you win and something like that so this way you can progress towards a minigame and create something interesting so let's go ahead and add some scoring mechanism to the game that will make it look even better so let's go to our code and here on the top I'm gonna create a score variables here we're gonna write int score which is which gonna be 0 in the beginning and whenever the on collision 2d happens and our tank collides with an enemy before destroying the enemy or after destroying the enemy what we can do is we can write score plus plus so score plus plus simply means we are incrementing the value of score so in the beginning the score was zero and when we will collide with the object the score will be 0 plus 1 that is 1 again when it collided the object the score will be 1 plus 1 that is 2 so this is how every time we collide with a game object we're gonna increase the value of score so now you will see that whenever we run the game nothing will be shown on the screen because here we have simply created a variable and we are increasing its value but it is not gonna show on the screen so in order to show it on the screen we have to create and uy text all right so to create a new I text you can go to create uy and either create a text or create a text messed so here I'm gonna simply create text but both of them gonna work so as you can see here we have the text and we can not see it in the scene so go to this gear icon on the right and click on reset and as you will see the position has been reset and now it is in the center now let's change the new text to zero so that it it shows our score now I'm gonna go to this alignment and Creek on this Center and center for both then I'm gonna go to this phone size and change it and make it much much bigger and as you can see if you double-click on the text we can see where it is on the screen so as you can see here it is on the screen I can click on this rect transform tool and move it to the top center something like this change the size like this and now I can simply go ahead and change the font size even more so let's make the size bigger like this and now let's change the font size something like this then we can change the color to white or red or anything that you would like to give let's give it a color like this or we can simply click on this color pick up tool and then we can click on any color in it it will automatically take that color so let's go ahead and click on this one and as you can see it took that color I can click on it here and click on this one as you can see it took that color again so this way I can change it to any color that I want I think I'm gonna keep it a color like this little more yellowish perhaps something like this so this should work so now this is what will store our score text on the screen so now we need to connect this UI element to our score value in the code so that we can show the code or show the score on the screen so let's rename it to score text now let's go back to our code and here we gonna write another public thing so here gonna write public game object not game object here we need to write public text not takes alignment public text score so we are creating a variable of type text but as you can see it is showing in red because the text does not exist in the current namespace so in order to use the UI elements here it write using unity engine not editor Indian dot Qi alright so now as you can see the error is gone so whenever we are going to use this using unity in general Qi namespace then we can use all the UI functionalities from it so this way here we have created a public text variable named score text now every time whenever we are increasing the value of score here we can write score text score text dot text equals score so now we are simply saying that the text property or the text value of our score text element will be set to this score value so if the score value is zero it will show zero on the screen if the score value is 1 it will show 1 on the screen if it is 2 it will show to other screen but the problem is this text is a string property and this school variable that you have created is an integer alright so we have to convert from integer to string in order to make this or write this code without any errors to do that the simplest way to do that is to write school dot to string and that's it so this will simply convert our school to a score integer to a string value and then it will set it as the text property of our score text so now everything almost done in the code now one more thing to do is we need to select our tank and as you can see our script is expecting the school text property here so here willing to simply drag and drop this score text element from our canvas right here so dragon drop this score text that is the score text that we have created to the canvas right here not to the canvas to the example scripts score text property right here so now our script has access to this score text element and now if I click on play you will see every time I go ahead and collect one object this fool will be incremented one two three and four so this way as you can see every time I collect an object the score gets incremented and this is how we can change the score and show it on the screen as well and all these things gonna be very very useful whenever you gonna create any kind of game so these are the useful and important things that you can use almost in every game that you create so now let's learn one more thing so when we destroy all the boxes we want to reload the game already start the game again so that we can play it from the beginning yield so whenever our school reaches four and we have destroyed all the boxes we want to reload or restart the game scene so in this case we have to restart the game scene so let's see how we can restart the game scene using C sharp so as you can see we have our scene named as game and if you go to file build settings as you can see our scene is not included in the build here so from here we can arrive we're gonna click on add open scenes and now as you can see our game scene is added to the build we don't need this sample scene that I have created before but make sure that whatever scene you are working with is added in the build settings otherwise it will not be able to load it from your C sharp code so now with that done let's go back to our code and in order to use all the functionalities for real and restarting the scene we have to use another namespace here so here in to write using unity engine door scene management nazi scene i'm sure what happened to see in management alright so now that we have written this dot scene management now we can use all the functions related to reloading the scene so here as you can see every time we are colliding with it we are checking that the by checking if the school is getting incremented so here we are simply incrementing the score and showing or displaying it on our screen so here we need to check whenever the school is greater than or equal to 4 then we want to reload our scene all right so for reloading our scene let's create another different function so that we can call it from anywhere that we want so here we're gonna create void restart so this is the function inside which we're gonna write our code to reload the scene so here we can write scene manager that load scene and what scene we want to load we want to load the scene name became that is our current scene so in your case the scene name probably be different so make sure you write it exactly as it is written right here or you can go to the scenes folder where we have saved the scenes and as you can see here this name of the scene is game so you can write it exactly as it is as it is written here otherwise it will not work so in my case whenever the restart function gets called this load scene function will get called and will load the game scene now we need to call this restart function from here alright so from here we're gonna check if score is greater than equal for because we have four different boxes so when we destroy all the four different boxes and our score is four or more then we want to call this restart function so now whenever our bank has destroyed all the four boxes and our score is greater than four the greater than or equal to four the restart function will get called and our game will simply be restarted so let's save the scene go back to unity and see how it's working so let's go ahead and destroy the blocks so one two three and four and as you can see as soon as I destroyed the fourth block the game has got restarted and we are here again so this is how we can call the different scene management functions to reload and restart the scene again now as you can see you will notice that whenever I touch this block exactly at that time the game gets restarted but I think that's not what he wanted what we want is whenever we win the game we wait for one or two seconds and - and celebrate our victory and after that we reload already start the scene so we need to learn a way so that we can call this restart function after a delay so we need to find some some way so that we can call this rich start function after one or two seconds with a delay all right so in unity it's very easy to do that by using the invoke function so first of all let me go ahead and comment this out because we are going to do it in a different way so here we can write invoke so this invoke function helps us to call any other function after a certain amount of time so first of all inside the string or inside double quotations we'll to write the name of the function that we want to invoke so here we want to use this breeze start so I'm going to select and copy it and it's a good idea to copy it because it will help us from preventing any errors oops not this one so restart copy-paste it and here so here as you can see first parameter we have keep and restart now for the second parameter we can give a comma and now we need to write after how much time we want to restart it so in this case let's say I'm to restart after two seconds all right so whenever the school is greater than equal to four we can wait for two seconds and then we're going to invoke the restart function and what does the restart function does it simply reloads the scene so now let's go back to unity and if I click on run and destroy the blocks so one two three and four one two so as you can see just after two seconds has passed our game gets reloaded again and restarted again and we can play it from the beginning one more time so let's go through it one more time so one two three four and restart so as you can see it waits for too many seconds and after that it simply restarts the game and reloads the game and as I have said before you need to make sure that in the file build settings you have added your current scene otherwise this reload functionality will not work alright so this way we have also learned how we can reload and restart the game we have learned how we can reload a level also we have to learn how we can add some delay so that we can reload our game after a little delay or after a specified amount of time so this way we have learned how to use say shop scripting in unity to do some essential things so all the things that I have shown you here you're gonna use them almost in every single game project that you're gonna create so these are some essentials functionalities that I wanted to talk about in this video so I hope you really enjoyed and learned a lot from this video so thank you so much so if you want to learn more and create some more cool projects you can check out my complete C sharp scripting for unity game development course in my course I have 25 plus hours of long videos and there you will get everything you need to create awesome games with C sharp and unity and everything that you need to learn about C sharp there you gonna learn about all the steel shop fundamentals all the unity fundamentals from absolute basics you will learn to create inputs for Android and mobile devices you will learn to implement unity ads you will learn to create achievement and lock systems and leaderboards and you will learn to create some basic game AI and along with that I will discuss about the intermediate topics like object-oriented programming and things like that also you're going to create two complete 2d and 3d games that you can show up to your friends or show in your portfolio and do anything that you want so this will give you a lot more experience so I hope you will really enjoy taking this course so you can check the link to my course from the description of this video and you'll get a huge discount if you take it from the link of the description so thank you so much for watching this video I hope you really enjoyed if you really liked this video make sure to give it a like and make sure to subscribe to this channel if you want to get more of these videos so thank you so much make sure to check out my course from the link in the description below and I'm going to see you in another video
Info
Channel: Charger Games
Views: 37,960
Rating: undefined out of 5
Keywords: unity c# tutorial, unity c# scripting tutorial, unity c# game development tutorial, unity c# tutorial for beginners, c# game development, c# for unity, c# tutorial for beginners, c# game development tutorial, c#, c# tutorial, c# programming, learn c#, unity android game tutorial how to make a game or app, charger games c#, c# game programming, unity C# move object, unity c# keyboard input, unity c# mouse input, unity C# course, unity 2019
Id: VkT4gIsr4JY
Channel Id: undefined
Length: 59min 42sec (3582 seconds)
Published: Sun Aug 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.