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;
}
}
}