My score system doesn't display or work? Please help me!
I am making a game that has a scoring system but it doesn't work. I am trying to make it where if the enemy dies the player gets 10 points and so on. So, I made a GUIText to display the score and made a script with the following:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Score : MonoBehaviour { public GUIText scoreText; private int score;
void Start()
{
score = 0;
Update();
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
Update();
}
void Update()
{
scoreText.text = "Score: " + score;
}
} and on the enemy side, I put the next following script so it can die when hit and to scores a point but it doesn't work.
using UnityEngine; using System.Collections;
public class Damagebycrash : MonoBehaviour { int health = 1; public int scoreValue; private Score score;
void OnTriggerEnter2D(Collider2D collider) {
if(collider.tag == "Invaders"){
score.AddScore(scoreValue);
}
Debug.Log("Trigger!!");
health--;
if (health <= 0) {
Die();
}
}
void Die()
{
Destroy(gameObject);
}
} so yeah please help me?
Answer by bcaloe · Feb 18, 2016 at 04:32 PM
First of all, "void Update()" method is called at every frame automatically. So there isn't really need to call Update () in your Start () and AddScore (int value).
Since i am lacking detailed information as to whether what is happening and what is not, I will be relying on my assumption based on the evaluation of only the code you posted.
I assume that enemies will die when collision is triggered. Correct? But I don't think your Score variable (private Score score) is assigned to anything yet. Hence, score.AddScore(scoreValue); will not do anything.
Try:
public static void AddScore(int newScoreValue) {
score += newScoreValue;
}
for your method in Score and you also need to make your score declaration to be "static int score"
void OnTriggerEnter2D(Collider2D collider){
if(collider.tag == "Invaders") {
Score.AddScore(scoreValue);
}
}
for your enemies' script.
This way, all enemies' scripts will have access to static method from Score script without having to assign the Score class variable.
Hope this helps : )
thanks, this helps little but the Score.AddScore(scoreValue); is causing an error ( "An object reference is required to access non-static member `Score.AddScore(int)'" ) and yes, enemy dies when the collision is triggered and score appears but when the player kills an enemy the score doesn't change, can you tell why and if you need more info ask me so you can help me and Thanks for helping me
Oh, I see why you are getting that error message. When you changed your AddScore method to be of type "static", you should also change the type of the variable being modified in that method to be of the type "static".
That will resolve the problem : )
Ok sorry for telling you this but when I kill an enemy the score doesn't update or doesn't change like at first it says Score: and then when I start the game it changes to Score:0 which is good because that the guiText script (Score) works. But when player kills enemy it says still says 0 here's the script for enemy and I fix the error thank you:
public class Damagebycrash : $$anonymous$$onoBehaviour { int health = 1;
static int newScoreValue;
public Score score;
void OnTriggerEnter2D(Collider2D collider) {
if (collider.tag == "Invader") {
Score.AddScore(newScoreValue);
}
Debug.Log("Trigger!!");
health--;
if (health <= 0) {
Die();
}
}
void Die()
{
Destroy(gameObject);
}
}
and here's the guiText script (Score)
public class Score : $$anonymous$$onoBehaviour { public GUIText scoreText; public static int score; static int newScore;
void Start()
{
score = 0;
}
public static void AddScore(int newScoreValue)
{
score += newScoreValue;
}
void Update()
{
scoreText.text = "Score: " + score;
}
}
Your answer
Follow this Question
Related Questions
How to add and track scoring in online multiplayer with UI? (Unet) 0 Answers
Anyway To Set A Specific Part Of Code To A Scene? 2 Answers
How to add one score every second to scoremanager c# 1 Answer
show score and keep score on screen until start new game? 0 Answers
How do I make a Score and Highscore thingie in game over screen 2 Answers