Okay.
I've got a bit of a problem and solution type lecture for us here.
And along the way we're going to learn about public access modifier and the problem that we've got.
I'm just going to put up my load level delay to one.
So we've got a little bit longer when we bump our head, what happens is when we bump our head, I'll
just spin around and bump.
I can continue to move and wobble.
I can hit the ground again, trigger a sound effect again.
So I've got a few things in here that we want to stop and prevent.
Basically, when the player crashes, I want to say you can't control your player anymore because you
crashed.
And I'm going to go about doing this in a way that allows us to understand public access modifiers.
So looking at this, theoretically, we've got our player controller script, which is saying how we
move our player, giving us the ability to rotate and speed up, etc., and we want to block that control.
However, we've got our crash detector that says, Hey, I know when you crashed.
So at the moment we've got a problem where these two scripts aren't speaking to each other and we're
going to use the public keyword to resolve that.
So let's jump into Visual Studio.
I'm going to close a couple of things here.
I don't need dust trail and I don't need finish line.
We're just working in player, controller and crash detector.
These are the two scripts and we've talked a little bit just very briefly about public versus private.
You've noticed each time Private has appeared, I've got there and said, Oh, I don't need private,
I'm going to delete that.
So what's going on here?
Well, by default, all of our variables and all of our methods are only available within this particular
class.
That is to say the scope of these things is local to this class.
There's some terminology for use scope, in other words, where you can use it.
And local to this class.
We've also talked about how if we've got something within these curly braces, for example, if we were
to go and define a variable within here, declare it and define it, then we can't use it outside of
this scope.
So those curly braces will define you can use something within here, but not elsewhere.
And we've talked a little bit about member variables that we can set up at the top of our class and
then use them anywhere.
As a point of comparison, the scope of these things is throughout our entire class.
However, how do we go about having something accessible from a different class?
So for example, if I'm in crash detector at the moment and I want to have a method in here that in
player controller we can call at the moment, we have no way of doing that.
If I try to call something in here, for example, the reload scene that I've created, if I were to
say call this particular reload scene method from player controller, we won't be allowed to do that
because reload scene is private.
Remember, it's private by default.
Even if you don't see the keyword in there, we would need to use the keyword public in order for us
to be able to call this method from other classes.
And why is it set up like this?
Well, it's safer that way.
It means that we don't accidentally go and call things.
It means that we are very explicit.
We're going out of our way to say, I want this thing or whatever we set up to be able to be called
from another place.
The same is true for variables.
So by default our variables are private, so private int whatever is our variable in here.
So there's a variable we could make this public and then we could change update modify this variable
from other classes or other scripts.
So for example, we'll make this ten and then if we're over in, so this is in the crash detector,
then over in player controller, we could access whatever and set whatever to 20.
It's probably not the best name, is it?
Whatever should name it something a little bit cleverer than that.
But this is our variable.
What you might see if you're cruising around on the interwebs, you'll see a lot of people use public
instead of using serialized field.
That is because if we set this to public, I'm just going to save my script here, jump back to Unity
and show you what I'm talking about.
Public is another way to allow us to see this in the inspector.
So if I click on Barry Fine crash detector, you can see here we've got whatever set to ten.
I could modify that in here.
So the public keyword or the public access modifier is another way to allow us to edit in our inspector.
However, I choose not to do it that way because this also means we can modify whatever from other scripts
and that can happen accidentally or it can be something that we modify, change, update and we don't
quite know exactly how it's happening.
So that is why Serialize Field is a safer way of going about doing that.
Now in our situation here, what is it that we would like to make public?
And I think we would like to have something within our player controller that is made public that says
anyone or any script.
I should say the script is not a person.
Rick, calm down.
So anywhere within our code base, within our project, we can call something on player controller to
say, you can't move anymore, we're disabling controls.
Let's go about creating that framework.
I'm going to pop underneath update.
We will add in a new method and I'm going to call this method.
Disable controls because that's what it's doing now.
In front of that, we're going to use our keyword public and we need to say what the return type is
and we don't have any return information.
We're looking to get back.
So we add void in here, we add our parentheses as usual, create our curly braces.
And what is it that we want to have happen when we when someone says, hey, disable the controls,
man.
Well, we want to literally not allow the player to do these things.
So there's a way we can do that by setting ourselves up a ball and using an if statement jump up to
the top of our script, we'll create a ball and we'll call.
This can move and we'll initialize that at true because you should be able to move.
Yes, you can move.
Now, a little micro challenge for you here.
I want you to go about using our ball to say that if you can move, then you can move.
But if you can't move, then you can't move.
So this is a micro challenge for you, see if you know how to do that.
Okay.
Hopefully that wasn't too vague.
It kind of felt a little bit vague.
But where we can do this is within update.
So I can type in if can move and by default that means it's true.
You don't have to say equals equals true.
But just know if you're saying if can move is if this thing resolves to true, then hey man, you can
go about rotating, it can go about boosting and then curly brace at the end.
So if you can move, then go about moving.
Great.
However, what we want to do is put some code in here with our disable controls to change that.
So another little micro challenge for you here.
What code can we add to our disable controls method so that if it's called, you can no longer move?
We will simply say can move the variable now equals false.
What is flicking the switch on our ball so it's false.
I'm going to save that and head over to crash detector, which is the place where we want to be calling
our new fancy public method.
And the first thing in here we need to say where the player controller is.
We need a reference to the player controller so we could set up a serialized field and say, this is
the particular player controller.
We could bring that in.
We can manually, manually drag it in the inspector, but we've just learned how to use find objective
type.
So let's use that one more time.
I'm going to under my if statement here, if the thing that I bumped into is the ground I'm going to
find object of type, what type do you think it's going to be?
It's going to be the player controller, which is the script, which is a class, but it's also a type
dot.
And we're going to call something in here.
What will we call?
You can see it listed right out there in all its glory.
This method we've created, which is public disable controls, parentheses, semicolon.
So we're hereby calling a public method.
I'm going to save that and just show you very quickly if I was to jump back in here and remove this
public keyword from disabled controls, save head back to crash detector, it's now like, hey, you
can't do this.
This is inaccessible due to its protection level, which is cool.
Shows that process.
So put public back in here, save it once again.
Crash detector should be cool.
Let us jump into unity.
Click on play and see if we can rotate after we crash.
So crash and I'm banging on the keyboard, but nothing is happening.
Excellent.
So it is successfully disabled our controls.
So just to recap on that, I know it's a new concept, but it's fairly simple.
Once you use it a few times when we have the public keyword, it simply means that we can access methods
from that class in other classes.
We can also do that for variables, but we tend not to do that.
We tend not to make public variables because they can get a little bit dangerous.
It's a lot harder to see if something's been changed as a variable than it is to see if something's
being called as a method.
Well, I think so.
Anyway.
So there we go.
We've learned a new thing which is using the public access modifier keyword.
I will see you in the next lecture.