Can't score points
Hello everyone, I'm new to Unity and I'm having problems with the score system of the Unity tutorial. Trying to make every dead enemy give the player 10 points and show it in the UI. I'm doing everything according to the tutorial but the value is not being shown. The Score Manager Script
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score;
Text text;
void Awake()
{
text = GetComponent<Text>();
score = 0;
}
void Update()
{
text.text = "Score: " + score;
}
}
The enemy's death script
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class EnemyScript : MonoBehaviour {
private float CurrentHPEnemy = 2f;
private float HPEnemy;
private bool dead;
public int scoreValue = 10;
public event System.Action OnDeath;
void Start () {
HPEnemy = CurrentHPEnemy;
}
public void HurtEnemy(float dmg){
HPEnemy -= dmg;
if (HPEnemy <= 0)
{
Die();
}
}
public void Die() {
dead = true;
if (OnDeath != null) {
OnDeath();
}
GameObject.Destroy(gameObject);
ScoreManager.score += scoreValue;
}
}
I tried to position ScoreManager.score += scoreValue; in the method public void HurtEnemy(float dmg) and in different ways but it also did not work
Any help will be appreciated and sorry for my english
Answer by Bluewatertech · May 25, 2018 at 07:38 PM
from what I can see, you need to reference where you're getting ScoreManager.score from. I'm not seeing any link to the two scripts in your code. Just do a public version of your script and drag and drop the gameobject onto your manager
public ScoreManager manager;
You could also get the game component as well.
score
is a public static variable so he does not need a reference to the Score$$anonymous$$anager
Your answer
Follow this Question
Related Questions
Cant keep OnGUI() elements to the same position C# 1 Answer
[HELP] To show wave number on a zombie survival game. 0 Answers
Scoring Points with UI Text 0 Answers
How to create a back button? 0 Answers