Page cover

Game manager

Now that we have our path manager set up and ready, we now need to create a game manager to let us switch between the different scenes once we're entering a level. For now, let's just focus on the scene transition effect before adding in the actual scene transition.

The setup

First, we need to create an empty game object and call it "Game Manager". We name it game manager instead of scene manager because the script that is going to be attached to this object is going to do more than just switching scenes later on.

Then we need to create a game manager script, attach it to the game manager game object and open it and you'll see the following.

We don't need "void Start" and void Update", so we can delete those and you should have an empty script now.

Now, let's create some variables that we need for our script to function. We want this game object to be available to us at all time, even when we switch scenes. To do this we need to make a static instance of itself.

Up next, we need a couple variables to control our fading effect we want to display before and after we switch scenes. For this we need a float value that controls the fading speed, an image reference to fade in and out with and a rect transform reference that we will use to display to the player what level node we're currently one. We'll add all this in Unity later, but for now let's focus on finishing of our game manager script. In the end your variables should look something like this.

We have a red line under "Image" because we didn't tell this script to use Unity's UI. Let's implemented this now at the top of our script to solve this issue.

Now, let's add the remaining variables we need in this script. We need a wait time value so the fading effect doesn't instantly get started. A reference to our path manager. A fade amount value to control our fading between black and opaque. An allow interaction boolean to control when the player is allowed to interact again. Lastly, we need a boolean to check if we're trying to fade to black or to opaque. In the end your variables should end up looking like this.

We now need to create an on enable function we're we can set this object to not be destroyed when switching scenes.

Then, we need to check if our game manager instance is equal to "NULL". This means we don't have the game manager instance set to itself yet.

If this is true, we need to set the game manager instance to itself and then set it not to be destroyed. We do this by making use of Unity's "DontDestroyOnLoad" function.

When this is not true, we need to destroy the game object to prevent having more then one game object since that can cause problems when executing certain lines of code you only want to execute once.

Lastly, we need to start our coroutine with the boolean parameter if we're fading to black or not. Simply add "StartCoroutine(FadeEffefct(isFadingBlack));" underneath the if statement.

Note: we have an error because we didn't create our coroutine yet, we'll solve this now.

And with that our on enable function is now ready. The next step is to create our fade effect coroutine that allows us to fade between opaque and black.

Fade effect

To create our fade effect, we need to give our "IEnumerator" a boolean parameter to check if we're fading to black or not.

First, we want to wait a bit before executing the rest of the code, so we'll add a "yield return" function and for the value we'll give it our "waitAmount" variable. Then we need to create a temporary color variable and set this to be a new color.

Note: The green line underneath or panel color variable means we're not using it anywhere. You can just ignore this as we'll be making use of it soon.

Next, we'll need to make an if else statement to check if our boolean "isFadingBlack" is true or not. Depending on the outcome we need to fade either from opaque to black or vise-versa.

Let's setup everything in our if statement first, where the outcome of our boolean is true.

When the boolean is true, it means we're currently going to transitioning from one scene to another. So what we need to do is set our alpha color value of our temporary color to zero and our allow interaction boolean to false.

Then, we'll need to make a while loop that keeps looping until the alpha color of the fading panel is no longer smaller than one. This means our panel has completely faded from opaque to black.

Inside our while loop we need to slowly increase our fade amount value over time. To do this we take our panels alpha color and add the result that comes from our fading speed multiplied by our delta time. Then we add those results to our fade amount value.

After that, we need to change our panel color value to a new alpha color value. To do this we set our panels RGB colors to be the same, but our alpha value needs to be our fade amount value we've just calculated.

Then we set our fade panel color equal to the temporary panel color value and return the alpha value of our fade panel.

After that we want to wait again before calling the same coroutine again to fade back from black to opaque. Inside our fade effect round brackets we pass through our allow interaction boolean so the function knows we're now trying to fade back from black to opaque.

That concludes the if statement for now, later on we need to come back in here to add another function.

Inside our else loop we almost do basically the same in our else loop, but with a couple changes. First, we need to set our panels alpha color to one.

Then, we can copy our while loop with our yield return underneath it from our if statement and paste in underneath our panel color.

Now we need to reverse a couple things, everywhere a "<" is placed should be an ">" and where a "+" is should be a "-". Then where we check if our fade panels alpha color is less than one, we should now check if it's greater than zero. The end result should look something like this.

Lastly, we need to check if our player can move around or not. To do this we need to call our "CheckUnlockingMovement" function via our path manager reference variable.

And with that our fading between black and opaque is now set up. We still need to come back to this to add some additional function once we've added those in.

Scene transition effect

We need a way to trigger our scene transition effect when we're entering a level. To do this we create another function that takes a boolean as its parameter to pass through if we need to fade to back or not.

Now all we have to do in here is to start our fade effect coroutine with the boolean value as its parameter we just got passed through.

And with that we've now successfully setup a way to trigger our scene transition from any script.

Toggle level info

We need a way to toggle our enter level display to notify the player that they are currently entering a level. We do this by creating a new function with a boolean parameter.

Now all we have to do here is set our game object active depending on the value it gets passed through.

Then we need to add this function inside our fade effect coroutine. We'll place this inside our if statement before we start our coroutine again.

That's all we need to do in our game manager script, but we still need to add this function to our player navigator script. We also still need to implement our functionality still in our player script to confirm the selected level. Let's set this up as well since we're working in the right script anyway.

Player confirm level entry

Let's add another function like we did for our direction input with the same parameter.

Now create an if statement that checks if the boolean can move is true or not. If it is we then need to set it to false inside our if statement. This is to prevent double firing the code.

After that, we need to call our scene transition function with a parameter of true so the coroutine knows we want to fade to black.

Then we need to set our world data assets current levels level data reference equal to the current level we're trying to enter, but we haven't set up that reference yet in our player script. Let's add this reference now.

Now we can go back into our on confirm function and set our world map current level data equal to our current level data. After that, we call our toggle enter level info function with a parameter of true to display the level info about the level we're currently entering.

With that our on confirm function is set up for now, but we'll come back to this later once we've set up the UI elements.

We now need to go back to our start function and change a few things.

Replacing the positioning on path

Since we've now got our scriptable objects set up and ready to use, our positioning on path function is now obsolete. We can remove this function from our start function and the function itself. We can also remove our can move is true setting since we don't have to set that in our start function anymore.

Before we can start setting our variables we need another vector3 variable to store our current level position in. That way we can set our stored current level position in our world data asset to our current level position on our player.

We have to create an if else statement to check if our current level in our world data asset is empty or not.

Let's do our if statement first, we need to set our transform position equal to our current level position. Then, we also need to set our current level position equal to the current level position.

With that our if statement is done. Now all we have to do is our else statement. When the current level inside our world data asset isn't empty, we need to set this as our current level position and set that value to be our transform position.

And with that our start function is complete. The last thing we need to create inside this script is an on trigger exit function to set our level information display to be inactive.

To do this we need to create an on trigger exit function and inside that function, we need to call our toggle event level info function from our game manager script and give it a parameter of false.

And with that we've not got everything set up for our game manager script wise. The only thing left for use to do is set the corresponding values via the inspector in Unity. But since we'll also need to make some UI elements, we'll set those values in that part of the tutorial.

Last updated