Something you might have noticed as you were creating your tower defense app is if you not only just
move the Tao around but you make the tower disappear like that and then a whole heap of grooves get
created after the fact and go to try and find a home.
A lot of the ones or actually all the ones that were created when there wasn't a home and were looking
for a home are wandering off into the distance and you can see them in more scene view.
Here they they're heading off what has happened is that the code itself it's OK if you're created when
there's no home.
But if you've found a home and it disappears you don't know it's disappeared and you don't keep looking
for one.
So the challenge in this is for you to fix that code to make sure that if the group is heading towards
a home and that home disappears before it gets there that when another home base becomes available it
will head towards that instead which will mean that you won't end up with any strike strike.
Google is going off into the distance so I pause a video have a go at fixing that.
And when we come back I'll show you a very quick fix to make it work.
So this functionality can all be done in sort of the move code.
Let's open that up and I'll go through where I've made the changes.
First of all we're going to need another tool which will act as a switch to say whether we're looking
for a home or not.
And then that can stop us from looking for a home constantly over and over again because the function
of doing that will need to reside in the update statement.
So here we have a ball looking for harm initially set to true because it's true that we will initially
be looking for a home.
Now the other changes I've got now I haven't changed the stuff I haven't changed the triggers or the
dead in a find home I'm setting looking for home to true inside of the excitement when a home is found
and that's to allow us to trigger that again.
When we come back around in the update statement and I'll show you where that is.
So just down in the update statement there is now an elf's if looking for home.
So if home is not able to know if there is a home base on the game board then head towards it or rotate
towards it.
If there isn't and we're looking for a home.
So this is set to true the last time we found a home which means this to execute when home becomes No.
It will then start invoke repainting for that find home which is what we were doing when we first created
the group.
At the same interval then they will sit looking for home to false what that's going to do is while this
invoker building is looking for the home looking for the home looking for the home.
This else won't keep executing and keep pushing this invoke repainting to happen because it will happen
or by myself.
And so therefore by including this bull we're ensuring that we only get these happening once until we've
found a home at which point was said about torture and that allow it to occur again.
OK so what does this now look like say that let's go back to unity in play.
So let's create some groups.
Get them wandering across the screen.
And we'll create a safe fear we already know that if you've got a lot that already exists and they're
going off the distance and we introduce our home they will come back to it.
So let's get no horn
let's bring them down there and I will now all come back.
And you can see in the hierarchy all those groups have now disappeared as they going into the tower
Now if I type this so why.
So let's just get rid of it and they will now start wandering off in whichever direction they're already
heading in.
The ones getting created right now haven't yet found a home so there are coyotes the ones that wandered
off dead into the bottom left hand corner that we're concerned about coming back.
So I bring down this now and we wait for a while the ones that went off by themselves that had lost
the home that they had will now start to wander back into the scene and he they come from those other
directions.
All right.
So that's now all working.
It won't end up getting any lost objects wandering off into the distance and out of your tower defense
game area.