In this video we're going to start
setting up our GameManager so that we can
test the BoardManager script that we just wrote.
Let's select our two scripts and add them
to the game manager.
Next let's open our game manager script in MonoDevelop.
In GameManager we're going to add
a public variable of the type BoardManager
called BoardScript.
We're also going to add a private variable
of the type int called Level and initialise it to 3.
We're using 3 because we're going to want to test
level 3 because that's where the enemies appear.
We'll change Start to Awake.
In Awake we're going to do two things.
We're going to get and store a component reference
to our BoardManager script.
And we're also going to call the InItGame function.
Let's declare InItGame.
In InItGame we're going to call the SetupScene function
of boardScript.
When we do this we're parse in the
parameter level so that we can tell boardScript
what level the scene that we're setting up
is so that it can determine the number of enemies.
With that done let's save our script and return to the editor.
Back in the editor it's time to assign
the variables of our BoardManager script.
Let's highlight the GameManager.
And what we're going to do is we're going to lock
the inspector so that we can then
click on multiple prefabs
and drag them in to our arrays here.
Let's lock the inspector by clicking the lock button up here.
And now what we can do, we'll start by dragging in
the exit, because that's first.
Next we're going to shift-click
to select all eight of our floor tile prefabs
and we're going to drag those directly
on to the floor tiles array.
This is going to allow us to add all of them at once
without losing focus.
Next we'll do the wall tiles.
Select Wall1, shift-click on Wall8.
Drag them to the Wall Tiles array.
We'll do the food tiles, in this case I'm going to
click on the food prefab and then command-click
on the soda prefab because I'm on a Mac.
If we were on a PC that would be control-click
to add to the selection.
We'll drag those to our Food Tiles array.
Let's grab our two enemy tiles.
And finally our outer wall tiles.
With those set let's unlock the inspector,
and give it a test.
Great, so we can see
that our levels are being spawned,
we can see that the camera is a little out of position,
so let's just fix that quickly.
We'll set the X to 3.5 and the Y to 3.5.
We'll also set the background to black.
With that done let's play the scene
and give it a test.
And so there we have it,
our exit, our floor background,
food items being spawned,
inner and outer walls being spawned,
and our enemies being spawned as well.
We're going to add some functionality to our GameManager script.
To make our GameManager what's called a singleton.
A singleton is an object for which there can
only be one instance in the game
at any given time.
Since the GameManager is going to do things like
loading levels, managing the player's score,
we wouldn't want more than one of
these object to exist,
so in our code we're going to make sure
that that's not possible.
Let's open the GameManager in MonoDevelop.
To setup our GameManager as a singleton
we're going to add a public static variable
of the type GameManager called Instance
and initialise it to null.
Declaring instances public means
that it will be accessible from outside the class.
Declaring it as static means that the variable will belong
to the class itself as opposed to
an instance of the class.
This means that we can now access the public functions
and variables of our game manager
from any script in our game.
In Awake we're going to check if
instance is equal to null, and if it is
we're going to assign it to this.
If that's not the case, and instance is not
equal to this we're going to destroy this
so that we don't accidentally end up
with two instances of our GameManager.
We're also going to set our GameManager
to DontDestroyOnLoad.
DontDestroyOnLoad means that when we
load a new scene, normally all of the
game objects in the hierarchy will be destroyed.
Because we want to use the GameManager to
keep track of the score between scenes
we don't want it to be destroyed at that point.
And so we're going to use DontDestroyOnLoad
to make sure that it will persist between scenes.
Let's save our script and return to the editor.
Now that we've added the scripts that we need to
our GameManager let's drag it down to our
Prefabs folder to create a prefab.
Before we're finished we're going to need one more
sample script which is going to load the
GameManager when the game starts.
We're going to go to Scripts and create
a new script called Loader.
Loader is a very simple script that's
just going to check if a GameManager has
been instantiated, and if not
instantiate one from our prefabs.
We're going to add a public game object,
called GameManager.
We're going to change our Start to an Awake.
In Awake we're going to check if GameManager.Instance
is equal to null.
Here we're using the static variable that we created
in the GameManager script
and accessing it from our Loader script.
If it is equal to null we're going to instantiate
our GameManager prefab.
We can delete the update function because we're not going to use it.
And that's all we need for now.
Now in the editor we can delete our GameManager from the hierarchy.
And add our Loader script to the main camera.
With our Loader script added we'll drag
in our GameManager prefab.
Now we can test our scene.
Now that we've got our GameManager started
and we can lay out some levels
it's time to get our units moving around.
In the next video we're going to write a script
called MovingObject to do that.
Subtitles by the Amara.org community