In this lecture, we're setting it up so you can pick up the package.
It disappears.
You can't pick up another package until you deliver that package.
Then after you deliver that, you can go pick up the next package and so on.
So let's get started.
First step we need to create a couple of extra packages to deliver and scatter them around our map.
So I'm going to click on a package control DX to duplicate and then move that to a different spot over
here and then control DX one more time and pop that over here as well.
Not trying to hide them too much yet we're still in the prototyping phase.
Side note, I think this circle, we don't need that anymore, so I'm going to delete that.
So I have three packages floating around in our scene for us to to drive over.
And if I click on one of these objects, if I want to hide this, I've got a few options.
First of all, I could just disable the entire object so it just goes away.
And that's one thing we can do.
That's totally fine.
I can also just turn off the Sprite renderer, but then I'd probably also have to turn off the box.
Collider So these are things that we can do as well.
We can access these components and say, do the equivalent of what I'm doing here, which is turning
the checkbox on and off.
However, for me, I don't want to use this package ever again.
Once I drive over it, I consider that it's been picked up and it's never going to appear in the game
ever again.
I want to do the equivalent of hitting delete and getting rid of that entire thing so it's no longer
sitting just there.
I'm going to undo that delete.
And to do that we can use destroy and I want to show you destroy because destroy is a handy thing to
know how to actually delete something from your scene and to destroy game objects.
We call destroy.
So we call destroy.
Specifically delete a game object from our scene.
Destroy when we call, it requires us to tell it.
In other words, pass in two things and we pass those things in to these parentheses.
So we have to tell it something.
What do we need to tell it?
We need to tell it which game object to destroy and how much time to wait until it is destroyed.
Now it's actually optional to pass in the delay.
If you don't have anything in there, then it will just destroy immediately.
But for the purpose of this challenge, I'd like for you to put in both the game object and the amount
of time you would like to pause until it is destroyed.
And so in our case, where we're looking to destroy something other than us, we would need to type,
destroy, and then in parentheses the monster or whatever the particular thing is that we want to destroy
dot game object.
So we need to say it's the game object we're interested in comma and then the delay.
So in this case I'm saying 0.05 F Now before I give you a challenge, I'll give you one or two other
clues in here.
So we need to destroy the package after we pick it up.
So we'll need to destroy it in a particular place.
And also, what are we destroying?
Well, the clue I'll give you is that this other is the game object.
So when we're getting the information, it's telling us the other thing.
It's starting off saying, here's the game object.
In other words, we'll call it other.
We'll name the game object that you bumped into other.
So there's a couple of hints for you.
Here's the challenge.
So destroy each package that you run over after 1/2.
Only destroy the one that you run over.
Don't destroy all of them.
But after you run over it, use destroy to remove that package so that we can pick them up and not have
them in the game anymore.
And I'd also like you to set up a variable and use serialized fields so that we can tune in the inspector
how long the delay is before the package disappears.
We don't need to go and find it in the code.
We can do that from in the inspector and say half a second or 2 seconds we can shoot it from there.
So there you go.
There's your challenge.
Take that on and I'll see you back here when you're done.
Okay.
So where do we need to destroy?
We need to destroy.
After we've gone and picked up the package, then we say, get rid of that package.
So I'm going to give myself a new line.
Destroy in parentheses.
I need to access other because that's the other thing we're interested in.
But I need to access the game object.
You see, the very first property we can get at here is game object.
So game object, that's the thing I'm destroying because Destroy wants to know the game object and then
comma, I can type in 0.5 F and then semicolon.
However, I challenge you not to have this magic number to serialize a variable, so I'm going to jump
up at the top of my class just above bool serialize field and then it will be of type what it's type
float.
We know that because there's an F down the end.
But if we mouse over here it's looking for destroys looking for a float.
T you can see just in here gives you a hint object and then float.
So we'll add a float and we'll call this destroy delay and I'm going to initialize that at 0.5 F, so
I'm going to give it a starting value.
It'll be at 0.5 F, then down here instead of 0.5 F, I will call this destroy delay.
Now let's save that back over into unity.
We'll make sure everything's hooked up the way we expect it to.
If we click on Cruisy MC Drive, we'll see.
Let's see.
Where is it?
Down the bottom here we have a destroyed delay currently on 0.5.
If I click on play, drive over.
Bup bup bup.
Yes.
After we touched it, there was a little bit of a delay, then it disappeared.
What happens with this one?
Yeah, it delays as well.
So they're all set up with that delay.
This is a good thing.
Just for me to note briefly, is that once you change something on a script, it applies to all the
places that script is being referenced.
So over here the delivery script is places a component and then it's looking at the information within
the script.
So you don't need to go and say, change the details of script, their details, the script, their
details, the script, they're the script.
If you're pointing to it from this game object, it will update in all those places just in case.
In case that hasn't been clear, I wanted to point that out.
Now you can see another problem.
The player can go and pick up a whole bunch of packages.
That might be fine.
You might want to let the player pick up all the packages, but for me, I don't want to do that.
So let's have a look at our logic here and say, how can we make it that the player can only pick up
one package at a time?
Well, what could we do up here saying if the other tag is package?
So if we drive a a package and something is the case.
Well, and what do we want to say in here?
We want to say, well, if you don't currently have a package.
And how do we write that?
Well, we could say has package equals equals false.
That would be one way of writing it.
However, there's a shorthand way we can write this is by saying in front of a house package, add an
exclamation mark, which is a way of saying not exclamation mark is not.
So we're saying here, if other tag is package is true and not has package is true, then do these things
which like I said before, is another way of saying has package equals equals false.
So let's check that that works back over into our game.
We want to pick out one package and then make sure we can't pick up any other packages now that we're
using our not operator.
Pick that one up yet?
Bit of a delay.
Try to pick this one up.
I won't let us pick it up because we already have a package.
Excellent.
So that's exactly what I wanted to do.
We can deliver there.
Pick up the next one.
Deliver their delivered backwards driving.
Pick up that one and deliver all of them exactly as we wanted to do.
Great stuff.
Okay.
That's all for this lecture.
I will see you in the next one.