In this lecture, we're going to give the play some visual feedback.
When I drive over a package which I've now changed to Green changes my car to green, then when I deliver
it, my colour of my car goes back to normal.
So let's go and change our car's color.
There's a couple of different ways we could approach changing out our player when we hit one of these
packages or the place we're trying to deliver it to.
One thing we can do over here in the Sprite renderer is we could change the actual image itself, so
we could change from this car sprite to a different car sprite.
But as you can see, I don't have any other car variations that are the same car as this, but a different
color.
I've just got these other completely different cars.
I don't think that would make sense.
When you pick up a package, you turn into some sort of, you know, yellow convertible.
It wouldn't make sense.
So and we don't have an artist to go and make this look pretty for us.
So we'll do something that's a little bit sneaky, but I think works.
Okay.
When we run over a package, we're going to change the color of the car.
So for example, we can put a tint on the car, let's see, to make it a green sort of thing.
But a green tint says, Yeah, you picked up a package.
And then when we deliver the package we can return the car back to its normal color.
So we're applying that sort of tint, but we're doing that in our code.
The way we need to do that is to access the sprite renderer.
Now what is a Sprite renderer?
It is a component.
The component sits on our player cruising MC Drive.
You see Sprite Renderer here as a component and on our component we have a number of different properties.
We have the Sprite, we have the color we have, whether it's flipped or not, and a few other things
in here that are part of this component.
So how do we access this Sprite render?
How do we go and change the Sprite renderer in our code?
We do that by using get component.
So let's open up our delivery script and I'll give you a head start on this, then I'll give you a bit
of a challenge.
What we want to do, first of all, is to pick a couple of colors.
Now it's a little bit difficult to know exactly what color we want.
So there's a little bit of a tip I'm going to give you here.
If we serialize our field and then create a variable of type color 32, that means there's the 32 bit
color.
Gives us lots of information you could do just color, but that's not going to be quite as nifty for
what I'm trying to show you here.
So color 32 and we need to give it a name.
So the first one I'll do is has package color.
I think and remember the way we write our variables is we do lowercase for the first word and then uppercase
for the other words, because this has a number of different values, actually.
You know what?
Let me just show you what I mean by that.
So when I go back and click on my crew's image drive changes color, you can see the here that we've
got a red, a green and a blue.
And as I move the slider around, those values are changing in there.
So it's not just one value.
We're not just saying the color announced a bit now needs to be a two or a 27.
We've got a whole bunch of colors in there and more specifically in the code we're going to be doing,
see how it says Rigby 0 to 255.
This is a common way that artists use ruby color.
There's also a setting in here which is Ruby 0 to 1 that's going to be anywhere from zero, which you
can see.
If I drag it all the way down to black, that's going to be zero on all the values, although out to
white is going to be one on the values, and somewhere in between is going to change on the particular
red, green and blue.
So it's between zero and one, which is a little bit friendlier for the computer to understand from
0 to 1.
So if I jump back over into my script here, we need to give it a new color.
And we can't just say in there like one comma, 0.2, comma, etc. We have to create a new color 32
so it knows what we're talking about.
Okay, you give me information for a color 32 and then in parentheses, this is where we can type in
back over to Unity.
We can type in the value of this, this, this and this.
So the IGB and a a standing for the alpha and I'm going to drag this all the way back up to white,
which means there's no tinting applied to my car, which means it's whatever color my art asset is already.
Remember, this is a tent we're doing here at the moment.
It's going over the top of our blue car.
So it's going to give you some interesting results.
You're not going to end up with exactly the same thing.
If we try to do a yellow, then it's going to end up with a greeny sort of color because it's over the
top of blue.
Okay.
So what we are looking for is just to start off with in our serialised field, one comma, one, come
on, one, comma, one.
And that will say, just start off with white or no tint and that's for the has packaged color.
Now little micro challenge for you here go ahead and create a second variable with the same structure
for a no package color variable.
So pause a video, give that a go.
Okay.
So we could do a copy paste.
Actually, what I'm going to do that I think we're at the stage now where you understand what I'm doing.
I'm going to highlight this line copy control C and then Control V to paste.
So it's exactly the same.
We need to go and change the variable name because we can't have these two variables exactly the same.
And we'll call this no package color and once again, we'll initialize that at this 1111, which is
white.
I'm going to save that for a moment.
We haven't yet got to the get component.
We'll get to that in a moment.
But what I want to show you, if I jump back over into unity, look for our delivery script.
Here's our delivery script.
You can see has packaged color and we get this little color selector in here so we can select a color.
And interestingly, it defaulted to black.
I was expecting it to default to white.
That's a bit of a weird one.
There.
You can see it's on 000 despite the fact that we initialize it at 1.11.
But not to worry, we'll just put it up to white and then we go.
We've got the package color and the no package color for now.
I'll just put the package color to just a bright green.
We can tweak that and change that later on, but there are no package color.
We want to leave the same.
Excellent.
So now they're the colors that we want to put on top of our car in our Sprite renderer.
So let's do that, go through that process.
So next what we need to do is create a variable where we can store the reference to our Sprite renderer.
So we want to say this is the Sprite renderer.
We're interested in this particular component.
We need to store it in a variable so we can use it over and over, use it wherever we want throughout
our script so we will type in Sprite Renderer.
This is the type.
It's this type Sprite Renderer and we'll call it just simply Sprite Renderer.
So it's the same thing.
No confusion there.
Lowercase s so the variable has a lowercase s and the type of thing has an uppercase s sprite renderer,
sprite renderer, variable and type.
Now that we've done that, we need to get it.
We need a way of saying this is the thing we're talking about.
So we'll add back in our start method because this is a place we want this to happen at the very start
of when we push play, we want our game to say, okay, where's the Sprite render?
Aha, there it is.
I'm just going to remove the keyword private and put my curly braces on a new line, get myself a tab
to move in here.
And now we need to say what is being stored in the Sprite renderer.
So Sprite renderer is our variable and that's going to equal and this cool thing called get component
that's saying go off and get this particular opponent component.
We need the angle braces which are just to the right of them on your keyboard.
And in here we're going to be getting which component do you think we're getting in here?
Well, we're getting the Sprite renderer, you guessed it.
Hopefully, maybe.
And then we need our parentheses because get component is a method that we're calling and then we hit
our semicolon.
So there we go.
That's the foundations for getting a component in the start method and storing or caching that component
in a variable.
And this variable is called Sprite Renderer.
Now we can use Sprite Renderer wherever we want.
From this point on.
It knows you are referring to this thing just here and as we've talked about before, I can use Sprite
renderer a dot something.
So Sprite renderer using the operator to access the properties from within here.
So this is a good point to have a bit of a challenge.
So I want you to change our cars color, find the right place in our code to update the color of the
car when it picks up and delivers a package.
And the hint I've got for you here is we need to assign our new color that we want to the Sprite Renderers
color.
So there's just a little bit of syntax for you to figure out in this challenge, but give it your best
shot.
Pause the video.
Take that on now.
I'll see you back here when you're done.
Okay.
So, first of all, let's find the place.
We want to do this.
I'm going to put this on the on trigger enter for the package.
So in here we're saying if the other tag is package, then we're going to pick it up.
And at this point, I want to change my color.
So where should we do it?
I don't like doing things after destroys.
It probably doesn't matter in this instance.
I'm going to pop this just under where we change our ball from.
Has package equals true?
It shouldn't matter.
But in here I'm going to start by accessing the sprite renderer.
This is the variable dot and I alluded to this color because we want to change the color.
What do we want to change it to?
When we pick up the package, it's going to be what is it has package color is what we want to change
it to and semicolon.
And then when we deliver the package similarly we want to change down here to.
Sprite renderer color equals no package color.
Let's save that.
See how we went with that process.
Remembering that the color I've selected was sort of a greenie type of color.
I think that one just there.
Yep.
Now let's click on play.
Drive over the package.
Boom.
And I've disappeared, which wasn't expected at all.
Let's figure out what's going on there.
What layer is my car on?
That's my first thing I'm going to look at.
I'll keep this error in just in case you're getting something similar.
Layer ten.
That makes sense.
Is it that we've made our car transparent?
That could be it as well.
Let's see the alpha.
That's right.
So the Alpha started off at zero for some reason.
It didn't show up when we were playing around with the color.
But Alpha Zero is not going to show up at I'm going to drag that all the way up to the right hand side.
I'm going to do the same for the other color all the way up to the right hand side.
Now, when I click on play, that was a bit of an unexpected one.
Let's see, driver that turns green, drive over that and turns back to blue.
Exactly.
But one area that I do have a disconnect now is when I pick up a package, I turn green, but the packages
themselves aren't green.
So maybe we'll just go and change the color of the package.
So click on one and then where are my packages?
There's one package.
Another package?
Where's the other package?
In my directory.
There, there, there's the final one is holding down control as I select that.
And you know what, I'm just going to drag this top package to be down in my hierarchy next to the other
packages.
There we go.
All the packages together, just click on the color, change it to a bright green.
It's matching the trees a little bit, but I don't know.
That might mean it's a little bit sneaky to try to find them and then just do a final test to make sure
that all looks as we want it to look.
When I drive a package, I turn kind of green.
When I deliver it, I go back to normal color.
There we go.
So we've updated our car to change colors when we drive over packages by using get component.
Okay, I'll see you in the next lecture.