Score increasing/decresing too much
i have a set number of enemy if all are destroyed the score should be the same every time, if i destroy them one at a time it works right if i destroy the quickly the scores are different each time and i don't know why
this is the code i'm using if there is some thing i'm missing please let me know
using UnityEngine; using System.Collections;
public class BulletCollider : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "FirstRankEnemy")
{
Destroy(gameObject);
Destroy(col.gameObject);
ScoreManager.score += 100;
}
else if (col.gameObject.tag == "WrongAnswer")
{
Destroy(gameObject);
Destroy(col.gameObject);
ScoreManager.score -= 500/2;
}
else if (col.gameObject.tag == "RightAnswer")
{
Destroy(gameObject);
Destroy(col.gameObject);
ScoreManager.score += 500/2;
}
else if (col.gameObject.name == "TopWall")
{
Destroy(gameObject);
}
if (col.gameObject.tag == "FortBrick")
{
Destroy(gameObject);
Destroy(col.gameObject);
}
}
}
and here is the score manager
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
static public int score;
public Text scoreText;
void Start()
{
PlayerPrefs.GetInt("scorePrefabs");
score = PlayerPrefs.GetInt("scorePrefabs");
}
void Update()
{
if (scoreText.name == "scoreText")
{
scoreText.text = "score: " + score;
}
}
void OnDestroy()
{
PlayerPrefs.SetInt("Score", score);
}
}
i think maybe the enemy is getting hit with more then one bullet but im not 100% is there a way to stop that form happening ??
im afraid im not familiar would that require a new code or would it go into $$anonymous$$e
Answer by stevehealy · Feb 10, 2017 at 04:15 PM
I changed OnTriggerEnter to OnTriggerExit and that fixed the problem i was having.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110758im_/https://answers.unity.com/themes/thub/images/avi.jpg)