- Home /
Making High Scores and scores via GUITexts
What's up Unitarians?
So I'm making this runner game, and I have a high score system - that should be working PERFECTLY, but it isn't. I have multiple GUITextures and GUITexts, and the score is raised by one every update, so your score can get pretty high. The code, named Entity, is pretty simple and self-explanatory. There are no errors in the console, so I have no idea what's happening.
using UnityEngine;
using System.Collections;
public class Entity : MonoBehaviour {
public float health;
public GameObject ragdoll;
//GUI Stuffs
public GUITexture GameOverGUI;
public GUITexture TryAgainGUI;
public GUITexture YourScoreWasGUI;
public GUIText ScoreGUIText;
public GUITexture GameOverBackgroundGUI;
//Score Stuffs
public GUIText HighScore;
public GUIText CurrentScore;
private int highscore;
private int score = 0;
public void Start(){
Instantiate(HighScore, new Vector3 (1.0f, 1.0f, 0f), Quaternion.identity);
Instantiate(CurrentScore, new Vector3 (1.0f, 0.9f, 0f), Quaternion.identity);
if (PlayerPrefs.GetInt("High Score Endless") == null){
PlayerPrefs.SetInt("High Score Endless", 0);
}
highscore = PlayerPrefs.GetInt("High Score");
HighScore.text = "Highscore: " + highscore;
}
public void Update(){
//Score Holder
score ++;
CurrentScore.text = "Score: " + score;
if (score > highscore){
highscore = score;
HighScore.text = "Highscore: " + highscore;
}
}
public void TakeDamage(float dmg) {
health -= dmg;
if (health <= 0) {
Die();
}
}
public void Die() {
ToggleVisibility();
Destroy(this.gameObject);
PlayerPrefs.SetInt("High Score Endless", highscore);
ScoreGUIText.text = "" + score;
}
public void ToggleVisibility(){
Instantiate(ScoreGUIText, new Vector3 (0.55f, 0.28f, 0f), Quaternion.identity);
Instantiate(YourScoreWasGUI, new Vector3 (0.43f, 0.3f, 0f), Quaternion.identity);
Instantiate(GameOverGUI, new Vector3 (0.5f, 0.65f, 0f), Quaternion.identity);
Instantiate(TryAgainGUI, new Vector3 (0.5f, 0.425f, 0f), Quaternion.identity);
Instantiate(GameOverBackgroundGUI, new Vector3 (0.5f, 0.5f, 0f), Quaternion.identity);
}
}
Are the GUITexture and GUIText objects getting spawned in your game - can you see them in the Hierarchy?
Answer by BlackHoleStorm · Apr 26, 2014 at 08:52 PM
You're trying to set your high score texts by just setting the int value, you need to add ToString to the end of it. For example
public int val;
public GUIText textToSet;
void SetString()
{
textToSet.text = "The value is: " + val.ToString();
}
Your answer
Follow this Question
Related Questions
Android: How do you position GUITexture and GUIText according to different screen sizes (JS)? 2 Answers
GUITexture and GUIText on android 2 Answers
How to rotate camera by GUI texture on android platform 1 Answer
Drawing UI text on GUI.DrawTexture , How to do it ? 1 Answer
Background hides text. How to solve that? -1 Answers