Hello guys.
Welcome to the video.
So in this one I want to talk about basics of C sharp.
And also I want to explain how Visual Studio is connected to unity and how they work together and help
us to make video games.
All right.
So we're going to start by discussing execution order.
I'm going to explain what it is.
How does it work and why do we need to know about this.
So in the Visual Studio over here we can see start and update.
And these two are part of execution order and part of Unity's life cycle, if to be more precise, it's
a part of Monobehaviours life cycle which was developed by unity.
This Monobehaviour is responsible for calling start method and this one is responsible for calling update
method.
You can call it function.
You can call it method.
Both are the same.
There is also a couple of other methods that you should know about.
So I'm going to type them on the screen awake and there is a fixed update.
Also, let me put a comment.
Then there is a start.
Then fixed update is called and then update after that.
There is much more to that actually.
And I'm going to quickly show you what I'm talking about.
But don't get overwhelmed by this.
Don't worry about this.
It's just like for you to know the general picture.
This is the entire cycle we have.
You can see a week on enable, reset, start, fixed update, and so many other things.
They all are important, but you don't need to remember them.
You don't need to memorize this.
You will learn it with practice for sure and not even all of them we're going to use during this course,
so it's not that important.
Let's just discuss these four because they are like most important I would say, and you should know
about them.
So let me briefly explain the difference between these four and how they are usually used.
A wake is called once before everything.
Then the start is called.
Then fixed update is called and then update is called.
And I'm going to say it again.
Awake and start is called only once to see the difference, let's just type debug log.
Awake was called.
Then let's go to start method and type Debug.log.
Start was called.
Then in the fixed update we're going to do debug dot log.
Fixed update was called and I'm going to be lazy.
This time I'm going to duplicate this line of code and move it down over here.
And I'm going to type.
Update was called and this line of code can be removed for now.
So we have four debug lines.
And now if we go back to unity and play the game we're going to see them in the console.
Let's go back.
If you don't have this window over here let me close it.
You can go to window.
General console and let's just drag it over here.
So over here we have a console.
And now I'm going to go to play mode.
And you can see awake was cold, start was cold.
It was done only once, then fixed.
Update is cold all the time and update is cold all the time, but much more often than fixed update.
So why it's like that and why do we need it like that?
Usually it's good practice to use a wake to set some relationships between scripts or getcomponent for
the script that it's going to use.
Basically, you know, do general setup for the game object.
Then if you want to do something only once and you want to initialize something, maybe set some default
values or maybe reset the score or something like that, you can do it in the start.
So anything you need to do once, you can do it in the start as well.
And this is what these methods are mostly used for then.
Fixed update is mostly used in the games where physical calculation is very important, where physics
are involved and you want everything to be even and stable because fixed update is called certain amount
of times per second, if to be precise.
By default we have value of 50 so called 50 times per second.
That's the default value.
And that is main difference from the update because update is called once per frame.
And if your game is let's say 200 fps, then it will be called 240 times per second.
And if your machine runs slower then it will be slower and it will be called less amount of times per
second.
And if you would set some physics calculations with the let's say cars movement in the update, then
on one computer on the slow computer car would move slower than on the fast computer.
So that is very important.
And if you have some game where physics are important and you expect some FPS drops, you expect this
game to run differently on different machines.
Then it's better to do movement in the fixed update and other things in the update.
What other things?
I'm talking about such thing as input check.
So what buttons player presses on the keyboard, or collision checks because you always want to be sure
you have fresh information about collision between the objects, or maybe some animation updates and
so on.
There are many things that can be done in the update, and some of them we're going to use later on
in the course.
As of now, what you should know is that update is called as often as possible.
So it can be used for things that you need to, uh, update as often as possible.
But fixed update has a fixed time step, and it's better to be used when you need some stable reaction
from anything you're trying to do.
However, there is sometimes exceptions and what to use for what it is always up to you, up to the
developer.
It always depends on the project and on the developer.
For example, in this video game, we're going to uh, we're going to update movement in the update
method, not fixed update.
And again I just say that if you want to update movement evenly and same on all of the machines, it's
better to do it in the fixed update.
But in our case we're going to have a very simple game.
I really doubt that this game will lag on any computer or any mobile phone.
In fact, this is the second time I making this project and I know it will not lag on other machines,
so it's going to be fine.
It will have high frame rate, so if we don't expect the FPS to drop, we can do it in the update.
Apart from that, this is a platformer game.
In the platformer games, it is very important to have responsive controls and it's very important to
have everything fast.
And you know again responsive.
So because of that we're going to do everything in the update because of that and also for educational
purpose.
And if in some cases we'll have problems with the performance we can always fix it.
But I'm telling you it will not happen for sure.
So we're not going to use fixed update.
We're going to use only update.
Also, for those who are familiar with unity a little bit, I just want to say that if you're using
Rb's velocity in the update, unity will take care of it and it will make sure the speed of the character
is consistent, even if your FPS is higher than before.
So we don't have to worry about anything here.
Okay, great.
Now that we got that out of the way, now let's discuss something else.
I'm going to remove this.
And for now I will remove starting awake as well.
Actually we can remove update as well.
We're not going to use it now, so let's remove it.
And let's talk about variables and let's talk about how do we make them, how we declare them, how
we assign the variable and why do we need them at all.
So as of now, if we go to inspector it's going to be empty.
There is nothing there on the player.
Right.
Wait a second.
Yeah, except the rigid body.
And it's feasible.
And I suppose we want to have a speed for the player so we know how fast he can run.
For that we would have to make a variable.
When you make a variable you need to define is it going to be public or private.
And I'm going to talk about public and private in a minute in the end of the video actually as of now,
just try to type after me okay.
Let's type public load.
Speed.
That's all what we need.
And let's save it.
Now you can see in the inspector we've got new field that says speed, and there is a number.
And if you drag it like so or type something in here you can see you make some number here.
And this can be used to define speed of the character.
It is like that because we declared a variable of a type float.
This is one of the data types that we use in in video games.
And it has name speed.
Basically you define what type of data you want to have in the can.
And let's say this one going to be float.
And then you can have lots of variables like that.
You can have tens or hundreds of floats.
So you need to know which one is which.
And for that we give it a name.
And for example we assign this as speed.
And it's just like that you make a variable of a certain type, then you give it a name.
In our case we have type float which is responsible for storing numbers with decimals such as 3.5,
1.2 and so on.
And it has a name speed.
This is how you declare a variable.
There is another data types.
There are lots of them.
But in this video I'm just going to tell you about main three data types that we're using public int
and let's call it as coins for example.
And this one is numbers without decimals.
This one is responsible for having the whole number.
Example.
Let's go here and just try to change the value and see what we have.
It can only be a number without dot, right?
And this is useful when you want to have maybe coins enemies amount anything like that because you can't
have 4.5 coins, right?
You can have only 4 or 5.
So this is the case.
It can be used for.
But for the speed it can be changed to any value.
Even with decimal.
There is another data type.
Let's do public bool.
And I'm going to name it as is that for example, can be true or false, and only that this is very
useful for controlling state of your character.
Like is he knocked away?
Maybe he's dead, maybe he's running, maybe he's falling.
All of that can be used with the help of booleans, and it can be only true or false.
True or false, nothing else.
There is another simple data type that is like out there, but it's not used much in development, especially
in the beginning.
It's a public stream player name, let's say.
And this one is text.
Simple as that.
If we save this and go back to unity.
Here you can just type Alex for example.
Right.
So this is for text.
And these are for simple data types.
And there are much more of them.
And we're going to use lots of them.
But don't worry about it.
Don't try to memorize everything.
You just need to know that there are different types.
And what is the structure here.
Like why do we type public?
Why do we type float?
Why do we type speed?
Because we decide if it's a public or private.
Then we decide what type of data we want to have.
Then we give it a name.
Simple as that.
And there again, there are lots of them.
There is public image, there is color, there is a transform, there is a animator.
There are so many of them.
And we're going to use most of them eventually.
So as of now, just don't worry about it and try to remember the structure.
The access type.
The data type and the name.
Now we have four public variables.
Let me delete two of them.
And this one is private.
I'm going to save it.
And let's go to Inspector.
And try to see the difference.
What is the difference between speed and coins right now?
Except that this is float and this is integer.
This is public.
And it can be.
Seen in the inspector over here.
And this is private, and it cannot be seen in the inspector.
It is not there.
We cannot see it.
This is one of the differences, and also public variable can be accessed from other script in the project.
Like maybe you have some, you know, player manager that can control speed of the character.
If that's the case, you need to make variable public.
But in general, it's good practice to make variables public only if you have a good reason to do so.
Other than that, it's better to keep variable private like we have here with the coins.
When something is private, it means it is available only to the script it was created in.
So if we make private variables inside of a player, they will be available only for player.
If we make public variable, it will be available to any other script in the future.
Okay.
And now I said that we need to keep variables private.
Correct?
Correct.
But we made Rigidbody public here because we need to assign it.
Right.
If we hide it and make it private, it will not be visible in the inspector.
And if it's not visible in the inspector, we cannot drag rigidbody here and assign it right.
That's the problem.
So for that case, we can open a wake.
And in their wake we can type RB equals to get component of rigid body 2D.
And this one will search for all of the components on this game object.
If the script is on this game object, it will search components on this game object and it will check
if there is a rigid body and if there is a rigid body, it will assign it to the RGB value.
So now we don't have to assign it manually.
We can just do it in the script and it will be assigned.
We can go to play mode now.
And then we can go here and click debug.
And you will see that in the player over here we have rigidbody assigned same as before.
Very cool right.
Very simple.
Now let's go back and do something else here.
Just a bit more of a clean up.
I'm going to delete these coins because we're not going to need them.
And let's discuss this speed value.
So we have to keep it private as well because we don't have a good reason to keep it public.
Let's make it private.
And now it is not visible in the inspector.
However, we still want to change the value.
We still want to be able to test different speed value on the player.
For that, we can go back to script and type here square brackets, serialized field.
And this will make it visible in the inspector and it will keep it private at the same time.
Let's save this.
And here we have speed that can be changed as well.
Now, you might ask me why not make rigid body serialized field and assign it in the inspector?
Yes, it can be done.
That would not be wrong.
But personally me, I prefer to have a less information in the inspector as possible.
If we can just assign IRB once and not to worry about it later on, it is better to hide it and assign
it in the script, because later on this player script will have lots of variables.
It's going to be maybe like.
That, you know.
So we need to hide as much information as we can.
Well, that's it for this video.
I know it was lots of information.
I understand you very well, but we don't need to know actually, anything else for now.
And we can start development of the character.
And we're going to do that in the next video.
Okay I'll see you there.
Bye bye.