Hey guys, I’m stuck at something that in my opinion should work. But hey, would’t life be too easy that way.
I want to update a variable when a character dies, and display this on screen. I currently have this in my EnemiesKilled script:
using UnityEngine;
using UnityEngine.UI;
public class EnemiesKilled : MonoBehaviour
{
public Text text;
public int KilledEnemies = 0;
// Use this for initialization
void Start()
{
text = text.GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
text.text = "Killed: " + KilledEnemies;
if (KilledEnemies > 50)
{
Time.timeScale = 0f;
//insert enemy killed penalty
}
}
}
And I have this in my other script:
public class EnemyScript : MonoBehaviour
{
public int EnemyHealth = 10;
public EnemiesKilled enemiesKilled;
void DeductPoints(int DamageAmount)
{
EnemyHealth -= DamageAmount;
}
void Start()
{
enemiesKilled = enemiesKilled.GetComponent<EnemiesKilled>();
}
void Update()
{
if (EnemyHealth <= 0)
{
enemiesKilled.KilledEnemies++;
Destroy(gameObject);
}
}
}
Can anyone tell me what is wrong with my code. In advance thanks! Also other tips would be really helpfull! My enemies won’t destroy anymore because of this part enemiesKilled.KilledEnemies++;