- Home /
Scoreboard will not update
I am brand new to unity and trying to make a simple scoreboard. When a collision occurs, however the scoreboard does not change. Please help. Here is part of the code:
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject enemy;
public GameObject laser;
public GUIText gamerscore;
float spawnTimer;
float shootTimer;
int score;
// Use this for initialization
void Start () {
Instantiate (gamerscore);
spawnTimer = 1.0f;
score = 0;
updatescore ();
}
public void addscore(int addnum){
score += addnum;
updatescore ();
}
void updatescore(){
gamerscore.text = score.ToString();
}
and
public class EnemyController : MonoBehaviour {
public float speed;
public GameObject explosion;
public int score;
GameController gameController;
// Use this for initialization
void Start () {
GameObject gameref = GameObject.FindWithTag ("MainCamera");
if (gameref != null) {
gameController = gameref.GetComponent<GameController> ();
}
if (gameref == null) {
Debug.Log("GameController not found");
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag.Equals("Laser"))
{
Destroy(other.gameObject);
Destroy(this.gameObject);
Instantiate(explosion, this.transform.position, this.transform.rotation);
gameController.addscore(score);
}
}
Does at least one of the objects have a Rigidbody attached to it?
@Ekta-$$anonymous$$ehta-D score by default as declared in the GameController class would be private and not accessible unless the access level was explicitly changed.
@sam2k13, try putting a Debug.Log statement in the if statement before the game controller call to see if OnCollisionEnter is invoked at all.
I did some debugging and the method add score is being called and score is increasing by 10. The issue is when the updatescore function is called from the addscore function it is not changing the guitext on the screen to the new string. I have noticed that sometimes after I exit out of game mode then the text will change.
Answer by alebasco · Aug 15, 2014 at 09:15 PM
The problem is that the gamerscore references the prefab, not the newly created gameobject.
When you write
Instantiate (gamerscore);
You are creating a new copy of that prefab, but you did not store that copy anywhere, so you do not have a reference.
Later, when updatescore() is called,
void updatescore()
{
gamerscore.text = score.ToString();
}
you try to change a value on the prefab, not the object you created.
Instead, you will need two variables
public GUIText gamerScorePrefab;
private GUIText gamerScore;
When you instantiate the object, instead write
GameObject newGamerScore = Instantiate(gamerScorePrefab.gameObject) as GameObject;
gamerScore = newGamerScore.GetComponent<GUIText>();
Then when you update the score,
gamerScore.text = score.ToString();
you are now referencing the object you created, instead of the prefab.
Your answer
Follow this Question
Related Questions
GUI Text Score counter 3 Answers
Help! Why doesn't this work? GUI/Static vars Help! Javascript 1 Answer
Coin Counting Problem 0 Answers