Our last points-related task is to update the BeetleNPC script so that appropriate points are added when a cherry hits a cucumber beetle. Here are the steps:
- In the Project panel, select the Custom Scripts | BeetleNPC
- Edit the script
- Add the following class variable: public PointsManager _ptsManager;
- Add the following lines of code inside the nested else if statement in the OnTriggerEnter() method:
_ptsManager = GameObject.Find ("Score_Value").GetComponent<PointsManager> ();
PointsManager.currentScore = PointsManager.currentScore + 10;
The two preceding lines of code create a reference to the PointsManager script and, on the second line, increment the currentScore by 10.
The updated BeetleNPC script's OnTriggerEnter() method should be as follows:
void OnTriggerEnter(Collider theObject) {
if (theObject.gameObject.CompareTag ("Cucumber")) {
cucumberToDestroy = theObject.gameObject;
BeetlePatrol.isEating = true;
animator.Play ("Eating on Ground");
StartCoroutine ("DestroyCucumber");
} else if (theObject.gameObject.CompareTag ("Cherry")) {
_ptsManager = GameObject.Find
("Score_Value").GetComponent<PointsManager> ();
PointsManager.currentScore = PointsManager.currentScore + 10;
BeetlePatrol.isAttacking = true;
cherryHit = true;
animator.Play ("Stand");
}
}