Hello, I’m trying create a scoring system which involves collecting coins to increase the score and colliding with rocks, which decreases the score.
I’m currently using this script:
var score = 0;
var scoreText = "Score: 0";
var mySkin : GUISkin;
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin") {
Debug.Log("Other object is a coin");
score += 1;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
function OnGUI () {
GUI.skin = mySkin;
GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }
The script works perfectly fine. However I’m unsure as to how to include a script which, on collision with a ‘Rock’, decreases the score. I’ve tried adding this to the original script, but to no avail:
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Rock") {
Debug.Log("Other object is a rock");
score += -1;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
}
}
Thank you for any answers or feedback.
-Ben