- Home /
HighScore Names
This is currently my script for a local high score system in my game. Everything works, but the inputting the names. What I want is for once you get to the screen with the high scores, you will be prompted to enter your initials (if you do get a high score). It then stores the initials with the correct score (which are two separate arrays). I'm just not sure how to get the string from the textfield that you enter to be paired with the score.
var position : int; // position to enter new hgih score
var positionName : int; // position where to enter name
var switched : boolean = false; // whether or not a score has been replaced
var HighS1 : GUIText;
var HighS2 : GUIText;
var HighS3 : GUIText;
var Name1 : GUIText;
var Name2 : GUIText;
var Name3 : GUIText;
var entName : String = "";
function OnGUI () {
entName = GUI.TextField (Rect (Screen.width/2 - 25, Screen.height/2 + 125, 50, 20), entName, 3);
}
function Start(){
HighScore();
}
function HighScore(){
for(position = 0; position < GlobalStats.High.length; position++){
if(switched == false && (GlobalStats.TotalScore > GlobalStats.High[position])){ // replace with new score
positionName = position;
switched = true;
for(curPos = GlobalStats.High.length - 1; curPos > 0;curPos--){ // shift scores to the right
GlobalStats.High[curPos] = GlobalStats.High[curPos - 1];
}
GlobalStats.High[position] = GlobalStats.TotalScore;
}
}
GlobalStats.HighName[positionName] = entName;
HighS1.text = GlobalStats.High[0].ToString();
HighS2.text = GlobalStats.High[1].ToString();
HighS3.text = GlobalStats.High[2].ToString();
Name1.text = GlobalStats.HighName[0].ToString();
Name2.text = GlobalStats.HighName[1].ToString();
Name3.text = GlobalStats.HighName[2].ToString();
}
Answer by Statement · Dec 05, 2011 at 10:45 AM
Maybe you could just use positionName and change the value each OnGUI? If your backing end storage is PlayerPrefs, then you probably should use another variable that guards writing to PlayerPrefs only when the user is done editing the name...
Oh and you probably need to reorganize the name array when you reorganize the score array..
var position : int; // position to enter new hgih score
var positionName : int; // position where to enter name
var switched : boolean = false; // whether or not a score has been replaced
var HighS1 : GUIText;
var HighS2 : GUIText;
var HighS3 : GUIText;
var Name1 : GUIText;
var Name2 : GUIText;
var Name3 : GUIText;
var entName : String = "";
function OnGUI () {
entName = GUI.TextField (Rect (Screen.width/2 - 25, Screen.height/2 + 125, 50, 20), entName, 3);
// ******************************************
// Just change the name directly, if feasible
// ******************************************
GlobalStats.HighName[positionName] = entName;
}
function Start(){
HighScore();
}
function HighScore(){
for(position = 0; position < GlobalStats.High.length; position++){
if(switched == false && (GlobalStats.TotalScore > GlobalStats.High[position])){ // replace with new score
positionName = position;
switched = true;
for(curPos = GlobalStats.High.length - 1; curPos > 0;curPos--){ // shift scores to the right
GlobalStats.High[curPos] = GlobalStats.High[curPos - 1];
// ***********************************************
// We probably need to reorganize the names aswell?
// ***********************************************
GlobalStats.HighName[curPos] = GlobalStats.HighName[curPos - 1];
}
GlobalStats.High[position] = GlobalStats.TotalScore;
}
}
GlobalStats.HighName[positionName] = entName;
HighS1.text = GlobalStats.High[0].ToString();
HighS2.text = GlobalStats.High[1].ToString();
HighS3.text = GlobalStats.High[2].ToString();
Name1.text = GlobalStats.HighName[0].ToString();
Name2.text = GlobalStats.HighName[1].ToString();
Name3.text = GlobalStats.HighName[2].ToString();
}
The problem is that the name won't show up until I replay the game. At the screen for highscores, when I enter a name, it doesn't show that it replaces the text in HighName[] until I come back to the high score scene again.
I know that I could fix this by getting the player to enter their name at the beginning of the game, but I really want them to enter their name when they reach the highscore scene.
Also it is not only replacing the correct HighName[] but then also takes the place of the next HighName[] when you play the next time. $$anonymous$$eaning if you play and enter HHH as your name it then stores it to the associated score, but then the next time you get to the highscore scene (and you got another highscore) then it will show that name is already being there.
Your answer
Follow this Question
Related Questions
how to put the name of a car ??? 0 Answers
How to changes font, color & size of the text in textfield? 2 Answers
Textfield doubt, multiplayer purpose... 2 Answers
TextField text component not updating 2 Answers