I’m using 3D Text for my scores. I have a C# script that collects the results of the alien ships getting killed in a public int variable gamePoints. I have a public Transform variable,gamePointsText, that equals the 3D Text object, that reads the gamePoints variable. When I first play, the gamePointsText reads the gamePoints variable, but does not update live. I can see in the Debug.Log that the scores are updating, but not in the 3D Text itselft. It seems to read the gamePoints variable once and that’s it, until I restart the game. Any ideas on how to make this update while playing? Thanks for any reply. Below is the C# script:
using UnityEngine;
using System.Collections;
public class GameControl : MonoBehaviour {
public int enemyMove;
public int enemyCount;
public float vertiMoveTime;
public int bulletCount;
public int playerArmor;
public int extraArmor;
public int gameCash;
public int specialWeap;
public int gamePoints;
public static Transform gamePointsText;
// Use this for initialization
void Start () {
gamePointsTex=GameObject.Find("ScoreAmount").transform;
}
// Update is called once per frame
void Update () {
gamePointsText.GetComponent<TextMesh>().text=gamePoints.ToString();
}
}
I wonder that this even complies, because in line 21. you have variable called “gamePointsTex” (missing t-letter) and on 17. 26. you have it “gamePointsText”
It’s hard to tell what’s going wrong when you post half the code.
But on a side note: Why do you call getPointsText.GetComponent() on every call, instead of saving the reference the TextMesh in the start method
Sorry about the typos. I have the Debug.Log in another script to see if any other script can read the public int gamePoints. If anyone has a solution or has some code I can have that works better, I am totally open. Thanks. Here is the correct code:
using UnityEngine;
using System.Collections;
public class GameControl : MonoBehaviour {
public int enemyMove;
public float vertiMoveTime;
public int bulletCount;
public int playerArmor;
public int extraArmor;
public int gamePoints;
public int gameCash;
public int specialWeap;
public int enemyCount;
public static Transform gamePointsText;
// Use this for initialization
void Start () {
gamePointsText=GameObject.Find("ScoreAmount").transform;
}
// Update is called once per frame
void Update () {
gamePointsText.GetComponent<TextMesh>().text=gamePoints.ToString();
}
}
It’s basically an event driven score system. You have an event in the score manager and each time the scores change, it will fire up an event.
Copy pasted from the previous one
public class Score {
private static Score instance = new Score();
private int currentScore;
public delegate ScoreUpdateHandler(Score score, int newScore);
public event ScoreUpdateHandler ScoreUpdate;
public Score Instance {
get { return instance; }
}
public void AddScore(int points) {
currentScore += points;
if(ScoreUpdate!=null)
ScoreUpdate(this);
}
}
...
public class DisplayScore : MonoBehaviour {
void Awake() {
Score.Instance.ScoreUpdate += delegate(Score score, int newScore) {
// Instead of guiText use your TextMesh
// guiText.text = newScore.ToString();
}
}
}