Quantcast
Channel: Questions in topic: "javascripting"
Viewing all articles
Browse latest Browse all 105

Player Prefs High Score User Name Entry?

$
0
0
Hello. How do I use playerprefs for name entries? The high score board is able to record up to 5 scores currently and I would like it to record the user name as well. I want the user to type their name in the StartMenu level and at the end of the game the highscore board will show the name and score of the player. It will rank based on the score. How do I go about doing that? Do I need a GUI text field for users to enter their name? How do I use playerprefs for names? I attached this to the "WinTriggeer" game object in the main game (Different level from StartMenu): function thescore () { var bonus : float = 100 - Time.timeSinceLevelLoad; var score : float = keepscore.score + bonus; var newName : String; var oldName : String; var nScript: NameEntry; newName = nScript.user; if( score > PlayerPrefs.GetFloat("Score1") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat( "Score2", PlayerPrefs.GetFloat("Score1") ); PlayerPrefs.SetFloat("Score1", score); oldName = PlayerPrefs.GetString("Name1", nScript.user); PlayerPrefs.SetString("Name1",newName); newName = oldName; } else if( score > PlayerPrefs.GetFloat("Score2") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat("Score2", score); oldName = PlayerPrefs.GetString("Name2", nScript.user); PlayerPrefs.SetString("Name2",newName); newName = oldName; } else if( score > PlayerPrefs.GetFloat("Score3") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat("Score3", score); oldName = PlayerPrefs.GetString("Name3", nScript.user); PlayerPrefs.SetString("Name3",newName); newName = oldName; } else if( score > PlayerPrefs.GetFloat("Score4") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat("Score4", score); oldName = PlayerPrefs.GetString("Name4", nScript.user); PlayerPrefs.SetString("Name4",newName); newName = oldName; } else if( score > PlayerPrefs.GetFloat("Score5") ){ PlayerPrefs.SetFloat("Score5", score); oldName = PlayerPrefs.GetString("Name5", nScript.user); PlayerPrefs.SetString("Name5",newName); newName = oldName; } PlayerPrefs.SetString("Name1",newName); newName = ""; } For users to enter their name in StartMenu: var user : String = "Enter Name"; function OnGUI () { user = GUI.TextField (Rect (7, 265, 100, 25), user, 25); //user enter name here thename(); } function thename () { if( user != PlayerPrefs.GetString("Name1") ){ PlayerPrefs.SetString( "Name5", PlayerPrefs.GetString("Name4") ); PlayerPrefs.SetString( "Name4", PlayerPrefs.GetString("Name3") ); PlayerPrefs.SetString( "Name3", PlayerPrefs.GetString("Name2") ); PlayerPrefs.SetString( "Name2", PlayerPrefs.GetString("Name1") ); PlayerPrefs.SetString("Name1", user); } else if( user != PlayerPrefs.GetString("Name2") ){ PlayerPrefs.SetString( "Name5", PlayerPrefs.GetString("Name4") ); PlayerPrefs.SetString( "Name4", PlayerPrefs.GetString("Name3") ); PlayerPrefs.SetString( "Name3", PlayerPrefs.GetString("Name2") ); PlayerPrefs.SetString("Name2", user); } else if( user != PlayerPrefs.GetString("Name3") ){ PlayerPrefs.SetString( "Name5", PlayerPrefs.GetString("Name4") ); PlayerPrefs.SetString( "Name4", PlayerPrefs.GetString("Name3") ); PlayerPrefs.SetString("Name3", user); } else if( user != PlayerPrefs.GetString("Name4") ){ PlayerPrefs.SetString( "Name5", PlayerPrefs.GetString("Name4") ); PlayerPrefs.SetString("Name4", user); } else if( user != PlayerPrefs.GetString("Name5") ){ PlayerPrefs.SetString("Name5", user); } } To show the name in the HighscoreBoard Level (5 different GUI texts): e.g. function Start () { guiText.text = (PlayerPrefs.GetString("Name1")).ToString (); } Currently it only shows "WinTrigger". Please help. Thank you. Cheers.

Viewing all articles
Browse latest Browse all 105

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>