[MUSIC]
Hello, I've come back in my time machine to give you one
piece of information about a bug we found.
And this bug is perpetuated throughout the entire course, but I've come back here to
show you what it is, so that you can not have that bug yourself.
It's a very rare sort of bug, it's not happening to a lot of people.
It's not happened to us as instructors as we've been prototyping and
creating the course.
But it's happening to enough people that we want to come and
make sure that you know about it.
So with our Singleton patent, we've just gone through this in the previous video.
We're finding a particular type of something,
in this instance my class is called Singleton.
So we're saying if there's more than one Singleton, then destroy yourself,
otherwise don't destroy yourself.
Now what we've finding through lots, many many hours of digging into this bug,
which is part of the game development fun, as a side note.
Is my teammate and fellow instructor Sam has helped
us work through the problem, why this is happening.
So this is the script execution order in Unity.
This is what happens first and second and the whole process and
what cycles, and this is what's happening per frame.
That you can see when we look for
it, OnDestroy's happening at the end of this first frame.
So it's happening sometimes when other things have gone and called that script.
So this line, we still need to destroy this game object,
so this Singleton knows there should only be one of me.
So that's all cool.
But sometimes, some other scripts that are relying on there only being one Singleton,
are having some issues.
Because there, for a very split second, a tiny tiny amount of time,
there are two until it gets destroyed.
So because this is being called a little bit later than we thought,
like a fraction of a moment later.
We need to do something before that to make sure that this very instant on Awake,
this thing can't cause any mischief.
And the way we do that is we make this Inactive.
So just an example, I've set up a little example saying this is my camera.
If I click the check mark, it's active, but unclick the check mark, it's inactive.
The moment I make it inactive,
then most of the things on this camera just won't work, won't happen.
And that's what we want to and wanna do when we're destroying it.
We wanna have it not have an impact.
So what we're saying here is, gameObject.SetActive(false); and
I want you to implement this now into the Singleton that we have just worked on.
So find Destroy, the line above Destroy,
set the game object to not active, so SetActive is false.
And I want you to do this from now on anytime we implement the Singleton patent
and you see me type in okay, now let us Destroy, then also put in this line.
Hopefully you remember it, but if you have any bugs, any issues,
any null reference exceptions when you're implementing the Singleton.
Then I know you'll remember this and will come back and
find this one line if you don't remember the one line itself.
Unfortunately, I can't go and re-record every single video that we've got this in.
So you'll see from now on, you'll only see Destroy.
You won't see this one particular line just cuz all the videos are re-recorded.
But I wanted to come back in time to right now to show you that this is the solution
if you're having any issues with your Singleton and the way that it's setup.
Hopefully, this should solve them for you.
Okay, great stuff.
Back to regular programming.
See you in the next video.