Adding a Score Multiplier
- 1. If you want the score to increase more if certain conditions are met, you can add a multiplier. For this example, we will cause each coin collected to increase the multiplier-- and, therefore, the amount by which score increases.
- 2. Select the "Score UI" object in your Project Hierarchy and view its Components tab. Click the gear icon on your ScoreCalculator script to view its code.
- 3. To be able to see the current multiplier, we need another region for text on the Score UI. To do this, we can change the inherited class from PickupUIBase to UIBase. We also need an integer variable to store the value of the multiplier. Edit the top of the class to look as follows:
public class ScoreCalculator : UIBase
int score = 0;
int multiplier = 1;
- 4. Save your script and view the Components tab. It will have updated with a new field called "Aux Text". Enter "1x" into this field.
- 5. In the ScoreCalculator script, we need to update the CalculateScore method to calculate score based on the multiplier. We also need to increase the multiplier every time the player collects a coin. To do this, change your CalculateScore method to the following:
public void CalculateScore(int value)
score += (10 * multiplier);
multiplier += 1;
Number = score;
Aux = multiplier.ToString() + "x";
- 6. Now, every time the player collects a coin, their score will increase by 10 multiplied by the multiplier, which will increase for every coin they get. The auxiliary text in Score UI will display the current multiplier underneath the score.
- 7. You can play test to ensure that score is calculating properly. You can also change the values for score and multiplier, or experiment with ways to change when the multiplier applies.