- Home /
Score not increasing after killing 1 enemy
Im trying to make a score system that increases each time a enemy dies, but instead it adds 1 time points and then it stays on 100, what might be the problem here?
private void OnCollisionEnter(Collision coll)
{
if (coll.gameObject.tag == "Bullet")
{
Death();
}
}
void Death()
{
Instantiate(DeathEffect, transform.position, transform.rotation);
Destroy(this.gameObject);
points = points + 100;
Text();
}
void Text()
{
score.text = "Score:" + points;
}
}
Answer by k234234w · Aug 06, 2019 at 06:03 PM
If points is a local variable on that script, every time an enemy collides with a bullet they will update the score text to 100 (and never past 100). The enemies do not know that points has been updated because they each have their own points variable. You could have a ScoreManager in the scene with its own points variable and every time an enemy collides with a bullet you tell the ScoreManager to update its points, and then set the text to the new value.
Answer by QaunainM · Oct 19, 2021 at 04:09 PM
The reason this normally happens is because you are destroying the GameObject
which has the script attached to it and that script is the element incrementing and storing the score
variable but as it's getting destroyed it's can't update the score counter.
One solution to solve this is to use Callback Actions.
Assume you have a Bullet
(which hits and kills enemies) and a Player
which launches the bullet
In your Bullet script
: Add this namespace at the top so you can use callbacks using System;
Then at the top of the Bullet script just above void Start() {} add this
public Action
onHitEnemy;` //this is used to receive an action when the bullet hits an enemy
Then inside the same bullet script at the end of your OnTriggerEnter (Collider other) code add
if (onHitEnemy != null) {
onHitEnemy();
}
Now inside your Player script
You need to also add the same namespace at the top so you can use callbacks using System;
Then add a variable for the score like this public int score = 0;
Then wherever you instantiate your bullet, inside that same block (i.e. inside the if
block that detects the user's button input to shoot the bullet) add this:
Bullet bullet = bulletObject.GetComponent<Bullet>();
bullet.onHitEnemy = () => {
score++;
};
//bulletObject is referring to the variable I have already created in this script which I use to instantiate and addforce to the bullet prefab.
The result of adding these new callback actions in the two different C# files for the Bullet and Player is that:
Every time your bullet fires (instantiated from the Player inside its Update method) it's looking to see if the onHitEnemy action is true.
When the Bullet detects a collision with the enemy via the OnTriggerEnter method it adds an action reference to the onHitEnemy method
When the two points above match it then means that score++; (aka score = score + 1;) can take place and this is stored inside the Player script so it doesn't matter if the bullet GameObject gets destroyed.