Score is not increasing after one game object is destroyed

So, in my game I have the score UI working but it only changes once and when my player kills another enemy it will change it to the enemy’s score points but it doesn’t add on to the total score or existing score. I have done a lot of researching but I cannot seem to find a solution to this issue. As an example score UI says 0 at the start of the game and when I destroy the enemy game object it posts the score points as 5 now when I defeat another game object it will display its reward scoring points 6 but it won’t add on to the current score of 5 making it 11. It just remains at 6 points. Is there a way to fix this issue? I have attached my code on this text file and pasted it on this forum if anyone wants to view it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
using TMPro; 
public class EnemyController : MonoBehaviour
{
    [Header("Unity Setup")]
    public ParticleSystem deathParticle;
    
    [SerializeField]
    Transform _destination;

    NavMeshAgent _navMeshAgent;
   
    public int hitPoints = 10;

    private int scorePoints;
    public int scoreValue;
    
    public int seconds = 1;

    bool isforceFieldActive;
    bool isScoreAdding;
    
    bool ifDead;
    
    public TextMeshProUGUI ScoreDisplay;


    // Start is called before the first frame update
    void Start()
    {
        scorePoints = 0;
        UpdateScore(0);
        _navMeshAgent = this.GetComponent<NavMeshAgent>();
        
        if (_navMeshAgent == null)
        {
            Debug.LogError("The nav mesh agent component isn't attached to  " + gameObject.name);
        }

        else
        {
            SetDestination();
        }
        ScoreDisplay.text = "Score:" + scorePoints.ToString();       
    }

    void Update()
    {        
        DestroyEnemy();                                             
    }

    //EnemyMovement
    private void SetDestination()
    {
        Vector3 targetVector = _destination.transform.position;
        _navMeshAgent.SetDestination(targetVector);
    }

    //Destroy the Enemy 
    public void DestroyEnemy()
    {
        if (hitPoints <= 0)
        {                   
            deathParticle = Instantiate(deathParticle, transform.position, Quaternion.identity) as ParticleSystem;
            
            StartCoroutine(Autodestroy());                      
            Destroy(deathParticle, 4.0f);
            
            ScoreDisplay.text = "Score:" + scorePoints.ToString();
            Destroy(this.gameObject);           
            ifDead = true;                      
        }
    }
    
    IEnumerator Autodestroy()
    {
        UpdateScore(scoreValue);
        yield return new WaitForSeconds(deathParticle.main.duration);
       
    }
 
    private void UpdateScore(int scoreIncrease)  
    {
        scorePoints += scoreIncrease;                  
    }

    void OnTriggerEnter(Collider other)
    {        
        if (other.tag == "Player")
        {           
            Debug.Log("Damage Success!");           
            other.gameObject.GetComponent<PlayerController>().lives -= 1;                       
        }
       
    }
    

}

When you do

StartCoroutine(Autodestroy());

That will run Autodestroy() later (next frame or maybe later this frame), but it won’t have the chance to run before you display the text…

Why have this in a coroutine? Can’t you just update the score immediately?

scorePoints += scoreValue; //set scoreValue to 6 before this, if that is what you want.

instead of

StartCoroutine(Autodestroy());

Problem solved…
Don’t use coroutines if it’s not necessary. Second row in your corourtine will accomplish nothing.

I tried it before and I was still getting the same results i changed the variable from scoreIncrease to scoreValue. It’s still not adding on to the existing score. is this what you meant?

 void Update()
    {        
        DestroyEnemy();
        if (ifDead == true)
        {
            scorePoints += scoreValue;
            ScoreDisplay.text = "Score:" + scorePoints.ToString();
        }
       
    }

public void DestroyEnemy()
    {
        if (hitPoints <= 0)
        {                   
            deathParticle = Instantiate(deathParticle, transform.position, Quaternion.identity) as ParticleSystem;
                             
            Destroy(deathParticle, 4.0f);
            
            Destroy(this.gameObject);           
            ifDead = true;                      
        }
    }

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.