Scripting defeat based on no cucumbers remaining

Our CucumberManager script already keeps track of the number of cucumbers in the game, so we merely need to give that script's currentCucumberCount class variable the static modifier and then update our VictoryManager script. Here are the steps.

  1. Edit the CucumberManager script so the currentCucumberCount variable declaration is as follows:

      public static int currentCucumberCount;
  1. In the Awake() method, change currentCucumberCount = 0; to currentCucumberCount = 1;. This will help ensure the game does not think there are no cucumbers when the game starts.
  2. Add the following statement at the end of the Update() method, currentCucumberCount = cucumbers.Length;. This will keep the counter updated in each frame.

Those were the only changes needed for the CucumberManager script. Next, we will edit the VictoryManager script:

  1. Edit the VictoryManager script by adding the int cucumberCount; class variable.

 

  1. Add the following lines of code to the bottom of the Update() method. These lines will continually check to see whether there are no cucumbers remaining and display the You Lost! text when the count equals zero:
         cucumberCount = CucumberManager.currentCucumberCount;

if (cucumberCount == 0) {
Victory.text = ("You Lost!");
}

You are now ready to test this defeat condition. 

You can speed up your testing by deleting cucumbers from the Hierarchy panel when in game mode. When you exit game mode, all deleted objects will be restored.
..................Content has been hidden....................

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