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 ...

Get Getting Started with Unity 2018 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.