So I think I made a big mistake with how I did this somewhere, but through a couple days of toying around and trying to get my score to work properly, I’ve finally gotten the scoreboxes to loop back with the obstacles in my game perfectly. The problem now is that when I hit a scorebox, rather than scoring a single point I’m gaining around 9 each time I go through one. I thought my code had an answer for this, but it doesn’t seem to be working. What I’m thinking is that because I have three different score related scripts throughout my game, maybe there’s some interference or something but I can’t for the life of me discern what could be causing it.
My first script is in the script for my character:
bool ScoreActive = true;
void OnTriggerEnter2D(Collider2D collider) {
if (godMode)
return;
if (collider.tag == "ScoreBox") {
if (ScoreActive == true) {
Score.AddPoint ();
ScoreActive = false;
}
} else {
animator.SetTrigger ("Dead");
dead = true;
}
}
}
The second code is my score script which I have set into a GUIText, obviously displaying the score to the player:
static int score = 0;
static int highScore = 0;
static public void AddPoint() {
score++;
if(score > highScore) {
highScore = score;
}
}
void Start(){
score = 0;
highScore = PlayerPrefs.GetInt ("highScore", 0);
}
void OnDestroy() {
PlayerPrefs.SetInt ("highScore", highScore);
}
// Update is called once per frame
void Update () {
guiText.text = "Score: " + score + "\nHigh Score: " + highScore;
}
}
And finally I have another score script in the scorebox itself, which is where I think the problem may be coming from:
void OnTriggerEnter2D(Collider2D collider) {
if(collider.tag == "Player") {
Score.AddPoint();
}
}
}
I’ve attempted to disable the last code and the problem still persisted, so I’m planning on getting rid of it but I’ve put it off just incase you guys think I should keep it or something. The code in my player is needed as it is what distinguishes that the score box isn’t a normal obstacle and prevents the player from dying when hitting hit.
Any help would be much appreciated!