For the challenge slide I took a different approach. I was trying to do some pre-planning based on the distance of the targets, (just as in the lecture) figuring that when a target is a certain distance away from the player, a greater score would be earned. I, however, thought of this as a boolean, in that I can later trigger a 3-pointer if the target is at least a certain distance away and would call a different method in the scorekeeper. Below is my code for the DetectScore and ScoreKeeper scripts based on the conditions of the boolean (currently public). To register 3-pointers, drag a prefab target into the scene and tick the Is Three Pointer check box to set it to True:
DetectScore.cs
using UnityEngine;
using System.Collections;
public class DetectScore : MonoBehaviour {
public bool isThreePointer = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision collision) {
ScoreKeeper scoreKeeper = FindObjectOfType<ScoreKeeper>();
if (isThreePointer) {
scoreKeeper.ThreePointer();
} else {
scoreKeeper.IncrementScore();
}
}
}
ScoreKeeper.cs
using UnityEngine;
using System.Collections;
public class ScoreKeeper : MonoBehaviour {
int score = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void IncrementScore () {
score ++;
print ("Target Hit. Your score is: " + score);
}
public void ThreePointer () {
score = score + 3;
print ("Target Hit. Your score is: " + score);
}
}