- Home /
Hello, I am working on a spaceship game and can't get my score to show or pop up. I need help.
public class HUD : MonoBehaviour {
public LifeIcon lifeIconObj;
// Stores the dynamically created instances of
// the life icons.
private List<LifeIcon> lifeIconInstances;
// Contains a reference to the Text Mesh used
// for the score text. Remember to drag drop
// the ScoreText game object on this variable
// in the editor's inspector window.
public TextMesh scoreTextMesh;
// This will contain a copy of the original text.
// When the text mesh is modified with dynamic
// values, we concatenate the numeric value with
// this string value.
private string scoreText;
// Update the score text to include the value passed
public void updateScore (int value) {
// Reset the text mesh's text to the orignal so
// the previous value is overwritten with the new one
scoreTextMesh.text = scoreText;
// Add the value to the text
scoreTextMesh.text += value;
}
public void CreateLifeIcons (int numLives){
// Has the life icon instance list already been created?
if (lifeIconInstances == null) {
// Create our list of life icons then populate
// it with the one visually placed in the editor
lifeIconInstances = new List<LifeIcon> ();
// Life Icon reference already exists so simply
// place it in the list.
lifeIconInstances.Add(lifeIconObj);
// The positional step to offset each instanced life icon
Vector3 positionalOffset = new Vector3(1.5f, 0, 0);
// Loop for each life past the first one and create a new
// icon for every life passed the first one
for (int i = 1; i < numLives; ++i)
{
// Create new life icon
LifeIcon newlifeIcon;
newlifeIcon = Instantiate (lifeIconObj,
lifeIconObj.transform.position,
lifeIconObj.transform.rotation) as LifeIcon;
// Apply positional offset
Vector3 newPosition = newlifeIcon.transform.position;
newPosition.x += (positionalOffset.x * i);
newlifeIcon.transform.position = newPosition;
// Reset the scale of the icon to its identify
newlifeIcon.transform.localScale = new Vector3(1,1,1);
// Store it in our list of instances
lifeIconInstances.Add(newlifeIcon);
}
}
}
// Updates the life icons' active status to reflect
// the number of lives remaining
public void updateLives (int numLivesRemamining){
// Loop for each life and create a new icon for
// every life past the first one
for(int i = 0; i < lifeIconInstances.Count; ++i)
{
bool bActive = i < numLivesRemamining;
lifeIconInstances [i].gameObject.SetActive (bActive);
}
}
// Use this for initialization
void Start () {
// Store a copy of the text mesh's text
scoreText = scoreTextMesh.text;
updateScore (10);
CreateLifeIcons (3);
}
// Update is called once per frame
void Update () {
}
}
Comment
Come on now, there isn't any information that is helpful in your post, Are you getting an error, is the component showing on the screen in the editor when in play mode? What is your expectation from the code above, something to help you with.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity - halo doesn't work all the time 0 Answers
Why does my Lerp only run once? 0 Answers
Enemy Ai is not working as planned 0 Answers