Implementing victory

In this section, we will implement the victory condition for the Cucumber Man. The only victory condition for the Cucumber Man is when the number of Cucumber Beetles is zero. Our BeetleManager script already provides functionality for counting Cucumber Beetles. As you will recall, that is how we update our Cucumber Beetle count on our HUD. We will make some modifications to that script and create a new script in this section.

Let's start by creating an onscreen text component to display You Won! when the number of Cucumber Beetles reaches zero. Here are the steps:

  1. Right-click an empty area of the Hierarchy panel.
  2. Select Create Empty.
  3. In the Inspector panel, rename the new GameObject to EndofGame. We will use this as a container for our victory and defeat text labels. 
  4. In the Hierarchy panel, drag the EndofGame GameObject to subordinate it to our HUD_Canvas.
  5. With the EndofGame GameObject selected, in the Inspector panel, select the Transform dropdown and click Reset. This resets the transform of the object.
  6. In the Hierarchy panel, right-click and select UI | Text
  7. Subordinate the new text object to the EndofGame GameObject.
  8. Rename the new text object Victory.

The next four steps are used to configure the Victory text object in the Inspector panel:

  1. Change the text property to You Won!
  2. Set the Font Style to Bold
  3. Increase the Font Size to 24
  4. Select a bright Color for the text

By clicking on the Game tab or putting the game into game mode, you can see the new victory text is displayed in the center of the screen. We only want that text displayed when the player has won the game. Let's tackle that task:

  1. Ensure the Victory text component is selected
  2. In the Inspector panel, click the Add Component button

 

  1. Select New Script and name the script VictoryManager
  2. In the Project panel, click Favorites | All Scripts
  3. Drag the VictoryManager script to the Assets | Custom Scripts folder
  4. Double-click the VictoryManager script to open it in an editor
  5. Edit the script so that it matches the following code, the first section of the code contains the namespace import statements and the VictoryManager class declaration:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VictoryManager : MonoBehaviour {

The next section of our script contains two class variable declarations and the Awake() method. In the Awake() method, we get the reference to the text component of our Victory UI object. We also set the initial text to null, so nothing will be displayed:

     Text Victory;
int beetleCount;

void Awake () {

Victory = GetComponent<Text> ();
Victory.text = "";
}

The last section of our script is the Update() method. Here we set the value of count to the current count of Cucumber Beetles, then test whether the count equals zero. If the (count == 0) condition is true, we display the victory text on the screen:

     void Update () {

beetleCount = BeetleManager.currentBeetleCount;

if (beetleCount == 0) {
Victory.text = ("You won!");

}
}
}

Our next task is to update the BeetleManager script. We will make three changes to the script:

  1. Add the static modifier to the currentBeetleCount class variable. The new line of code should be: 
      public static int currentBeetleCount;

  1. In the Awake() method, change currentBeetleCount = 0; to currentBeetleCount =1;.  This will help ensure the game does not think there are no Cucumber Beetles when the game starts.

  2. Add the following statement as the final statement in the Update() method: currentBeetleCount = beetles.Length;. This will update the currentBeetleCount variable for each frame.

You are now ready to test the game. Kill all your Cucumber Beetles to test the code changes you made. If something does not work correctly or you receive errors, please refer to the following updated BeetleManager script:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BeetleManager : MonoBehaviour {

public static int currentBeetleCount;
Text Beetle_Count;
public GameObject[] beetles;

void Awake () {

Beetle_Count = GetComponent<Text> ();
currentBeetleCount = 1;
}

void Update () {

beetles = GameObject.FindGameObjectsWithTag ("Beetle");
Beetle_Count.text = beetles.Length.ToString();
currentBeetleCount = beetles.Length;
}
}

Now that the victory condition has been implemented, we are ready to implement our defeat conditions. We will do that in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset