How do I make a score system?
I do the tutorial score manager but when I do everything it does not work. PLEASE HELP.
Here's the Enemy Health -
using System.Collections;
using UnityEngine;
public class EnemyHealthManager : MonoBehaviour {
public int MaxHealth;
public int CurrentHealth;
public int scoreValue = 10;
// Use this for initialization
void Start()
{
CurrentHealth = MaxHealth;
}
// Update is called once per frame
void Update()
{
if (CurrentHealth <= 0)
{
Destroy(gameObject);
}
}
public void HurtEnemy(int damageToGive)
{
CurrentHealth -= damageToGive;
}
public void SetMaxHealth()
{
CurrentHealth = MaxHealth;
}
public void Scoring()
{
ScoreManager.score += scoreValue;
}
}
Here's the ScoreManger -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score;
Text text;
void Awake()
{
text = GetComponent<Text>();
score = 0;
}
void Update()
{
text.text = "Score: " + score;
}
}
using System.Collections;
using UnityEngine;
public class EnemyHealth$$anonymous$$anager : $$anonymous$$onoBehaviour {
public int $$anonymous$$axHealth;
public int CurrentHealth;
public int scoreValue = 10;
// Use this for initialization
void Start()
{
CurrentHealth = $$anonymous$$axHealth;
}
// Update is called once per frame
void Update()
{
if (CurrentHealth <= 0)
{
Scoring();
Destroy(gameObject);
}
}
public void HurtEnemy(int damageToGive)
{
CurrentHealth -= damageToGive;
}
public void Set$$anonymous$$axHealth()
{
CurrentHealth = $$anonymous$$axHealth;
}
public void Scoring()
{
Score$$anonymous$$anager.score += scoreValue;
}
}
And call HurtEnemy from another script. Or change CurrentHealth to 0 in inspector
Answer by SohailBukhari · Apr 12, 2017 at 07:56 AM
Updating score in Update() will create a new memory block each time when you change score , so its not good approach to update scoring. Make a unity event and listen in your ScoreManager class as in the script.
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
Text _text;
[NotNull] public static UnityEvent ScoreUpdateEvent;
public static int Score;
private void Start()
{
_text = GetComponent<Text>();
ScoreUpdateEvent.AddListener(delegate
{
_text.text = "Score: " +Score;
});
}
}
Now your ScoreManager class is listening ScoreUpdateEvent you just need to invoke the event whenever you change the score.
public void Scoring()
{
ScoreManager.Score += scoreValue;
ScoreManager.ScoreUpdateEvent.Invoke(); // invoke here
}
Yeah it now works but now there's a issue with the game over scene, when it changes scene, the mouse is still locked on
Check your time.timescale in the code, may be you assign time.timescale to zero and never change to 1.
check your setting of mouse. Its not related with these scrips. your question was about scoring system and my answer helps you. Just accept the answer.
Your answer
Follow this Question
Related Questions
How do I add a score system? 1 Answer
Problem with ScoreBoard in Ping Pong game 1 Answer
How do I make the Score system work? 1 Answer
Problem with counting score in 3D game 1 Answer
Highscore table C# HELP!!! 0 Answers