Infinite Roller Tutorial Thirteen – Blocks Part Three

Welcome to Edge of Code’s series of Infinite Roller tutorials. If you’d prefer a video tutorial, you can find one here.

In the previous tutorial, we continued using blocks to create terrain and started adding to the menu. Today we’ll make the menu buttons play the appropriate game and display a separate high score for each terrain type.

Open the Infinite Roller project and the scene. First we’ll make the buttons do what we want them to. Open the GameController script and create a new method that takes the TerrainType as a parameter.

/* Set the currentTerrainGenerator variable*/
public void SetCurrentTerrainGenerator(TerrainType terrainType)
{
}

We need to be able to deactivate and activate the appropriate terrain generator. To do this, let’s create a method in the AbstractTerrainGenerator script. Open it, and add

public void Activate(bool activate)
{
	gameObject.SetActive(activate);
}

Unlike the Restart method, since the code is the same for both of the subclasses, we don’t need to use an abstract method. SetActive either activates or deactivates the gameobject dependant on what is passed into the method.

Unity tutorial activate

Save the script and go back to GameController. Add the following to the SetCurrentTerrainGenerator method:

//deactivate the existing terrain generator
currentTerrainGenerator.Activate(false);
	
//set currentTerrainGenerator
if (terrainType == TerrainType.Chunks)
{
	currentTerrainGenerator = chunkTerrainGenerator;
}
else
{
	currentTerrainGenerator = blockTerrainGenerator;
}

//activate the new terrain generator and restart
currentTerrainGenerator.Activate(true);
currentTerrainGenerator.Restart();

Save the script.

Unity tutorial set terrain

Next, open MenuManager and create a new variable to reference the GameController

public GameController gameController;

Save the script and go back to Unity. Select the MainMenu gameobject and drag the GameController gameobject onto the empty slot. Return to the script and call the SetCurrentTerrainGenerator method from each of the Play methods. Remember, these are the methods the menu buttons call when they are pressed. Add

gameController.SetCurrentTerrainGenerator (GameController.TerrainType.Chunks);

to the PlayChunksGame method above ToggleMenu() and

gameController.SetCurrentTerrainGenerator (GameController.TerrainType.Blocks);

to the PlayBlocksGame method, again above ToggleMenu().

Unity tutorial hook up buttons

Save the script and return to Unity. Select the BlockTerrainGenerator gameobject and click the ticked box in the upper left corner of the hierarchy. This deactivates the gameobject. Do the same for the ChunkTerrainGenerator gameobject. This will allow the SetCurrentTerrainGenerator method to work correctly. Play the game to check each button does what is expected.

At the moment, the displayed high score is the same for both games. We’re going to fix that now so that the high score for the block game is separate from the high score for the chunk game. First, open HighScoreSetter and add comment out the second and third lines of code in Awake so that we have

void Awake()
{		
	text = GetComponent<Text>();
}

Create a new method called NewGame with a TerrainType parameter. Inside, we’ll add code to give a different highScoreKey for each terrain type and then set the high score text:

public void NewGame(GameController.TerrainType terrainType)
{
	highScoreKey = Application.loadedLevelName;
	if (terrainType == GameController.TerrainType.Chunks)
	{
		highScoreKey += GameController.TerrainType.Chunks;
	}
	else
	{
		highScoreKey += GameController.TerrainType.Blocks;
	}
	
	SetHighScoreText();
}

The += adds either Chunks or Blocks onto the existing highScoreKey value (the scene’s name), and SetHighScoreText gets the game’s high score and displays it.

Unity tutorial separate high scores

Save the script and go back to MenuManager. Add a variable to reference the HighScoreSetter class,

public HighScoreSetter highScoreSetter;

then save the script and return to Unity. Drag the HighScore gameobject (which is a child of the HUD gameobject) onto the empty slot. Go back to the MenuManager script and call NewGame from each play method:

public void PlayChunksGame()
{
	gameController.SetCurrentTerrainGenerator (GameController.TerrainType.Chunks);
	highScoreSetter.NewGame(GameController.TerrainType.Chunks);
	ToggleMenu();
}

public void PlayBlocksGame()
{
	gameController.SetCurrentTerrainGenerator (GameController.TerrainType.Blocks);
	highScoreSetter.NewGame(GameController.TerrainType.Blocks);
	ToggleMenu();
}

Save the script, go back to Unity and press play to check that the high score is different for each terrain type.

Save the scene and the project. In the next and final tutorial, we’ll generate the random block terrain.

Remember you can download the files for this tutorial on the Downloads page. See you next time!

PreviousTutorialButtonNextTutorialButton

Comments are closed.