To set the winning conditions for the level, we need to count the items collected and check that value against a default number of items we want the level to end with. Once the number of items collected equals the goal, the player receives a message.
This is the topic of our next recipe.
We have already counted the number of items collected in the runner
script, through the collected
int
variable. Thus we can take advantage of the code we already implemented and improve it to manage the additional functionality.
Open the
runner
script in Monodevelop. In the upper section with the variable declaration, modify the declaration forcollected
, as shown here:public int collected, levelGoal;
In the
Start()
function, add the initialization forlevelGoal
with the value of5
:collected = 0; levelGoal= 5;
In the
Update()
function, add the following lines to check whether the winning conditions are met, and send a message to the player...