Why am I getting a negative score?

Hello I’ve been working on a simple scoring script. The problem is that I’m somehow getting negative scores. When the score is 0 and there’s a collision with a rock it doesn’t yield a negative score. But when the score is, say, 40 a collision with a rock will make it -80. Here is the script:

var score = 0;
var DeductSlowDownScore = 110;
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 += 160;
        scoreText = "Score: " + score;
        Debug.Log("Score is now " + score);
        Destroy(other.gameObject);
     }
    	else if (other.tag == "Rock" && score > 0) {
    	Debug.Log("Other object is a rock");
    	score += -120;
    	scoreText = "Score: " + score;
    	Debug.Log("Score is now " + score);
     }
    	else if (other.tag == "Tree" && score > 0) {
    	Debug.Log("Other object is a tree");
    	score += -95;
    	scoreText = "Score: " + score;
    	Debug.Log("Score is now " + score);
	}

}

function OnGUI () {
    GUI.skin = mySkin;
    GUI.Box (Rect (140, 10, 500, 200), scoreText.ToString());
    if (GUI.Button (Rect (5, 5, 110, 60), "Slow Down")) {
        StartCoroutine( SlowDown( 1.5 ) );// deactivate for 1.5 secs
      
    }
}

function SlowDown(deactivateInSeconds : float)
{
    var script4 = GetComponent("Raft Forward - Hard"); 
    script4.enabled = false;
    var script5 = GetComponent("RaftForwardEasier - Hard"); 
    script5.enabled = true;
    var script6 = GetComponent("Score - Hard"); 
    script6.enabled = false;

  
    yield WaitForSeconds(deactivateInSeconds);

    script5.enabled = false;
    script4.enabled = true;
    script6.enabled = true;
	score = score - DeductSlowDownScore;
    scoreText = "Score: " + score;
	Debug.Log("Score is now " + score);

};

I’m note sure where I’ve gone wrong. Many thanks for any answers or feedback. -Ben

As Fattie said, you’re actually subtracting the score here. If that is what you intend, the reason you’re getting negative is that before you do the subtraction you’re checking to see if the score is > 0. If, for example, the score is 100, it’s going to pass your Score > 0 test, but then subtract 120, which will give you -20.

To resolve this, I would go ahead and subtract the score in all instances. Then after all of your various tests add this:

if (Score < 0)
{
  Score = 0;
}

Try this:

function OnTriggerEnter(other : Collider) {
    Debug.Log("OnTriggerEnter() was called");
    if (other.tag == "Coin") {
        Debug.Log("Other object is a coin");
        score += 160;
     } else if (other.tag == "Rock") {
        Debug.Log("Other object is a rock");
        score = Mathf.Max(0, score - 120);
     } else if (other.tag == "Tree") {
        Debug.Log("Other object is a tree");
        score = Mathf.Max(0, score - 95);
    }

    scoreText = "Score: " + score;
    Debug.Log("Score is now " + score);

}