Search Unity

Player Name in Highscore

Discussion in 'UGUI & TextMesh Pro' started by NoM16, Sep 26, 2019.

  1. NoM16

    NoM16

    Joined:
    May 29, 2019
    Posts:
    2
    Hi Guys i have a problem setting the Playername in my Highscore Canvas. I am creating a game for Oculus Quest, with Unity 2019.1.14f1.

    I have two diffrent Scenes, a Main Menu Scene and a Game Scene where i collect some Coins, they have a counter and everything works fine. I do have a Canvas where i put in my Name, bevor I start the Game. Then the scene switches to Game and turns back after 90 seconds in the main menu.
    Code (CSharp):
    1.   void GameOver()
    2.     {
    3.         if (timer <= 0)
    4.         {
    5.            
    6.             if (PlayerPrefs.GetFloat("Score") < count)
    7.             {
    8.                 PlayerPrefs.SetFloat("Score", count);
    9.             }
    10.             SceneManager.LoadScene("StartMenü");
    11.         }//Load a new scene
    12.     }
    That is the part of the Code i attached to the Game Scene.
    The Count gets Displayed in the Highscore Canvas text field, correctly.

    Now I want to set the Playersname as well, when a new Highscore is Set.
    But first of all the Name doesnt get displayed in the Textfield name.

    Here is my Code in the Main Menue Scene , any suggestions how to solve that problem . Sorry for my bad englisch. My programming skills aren't the best as well.

    Code (CSharp):
    1. public class Highscore : MonoBehaviour
    2. {
    3.     public Text highscoreTextfirst;
    4.     private int score;
    5.  
    6.     public Text SpielerName;
    7.     public InputField iField;
    8.     private string SpielerNamestr;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     private void Start()
    13.     {
    14.         score = (int) PlayerPrefs.GetFloat("Score");
    15.         highscoreTextfirst.text = score.ToString();
    16.     }
    17.  
    18.     /*  void SpielerN()
    19.       {
    20.           SpielerNamestr = iField.text;
    21.           PlayerPrefs.SetString("Spielername", SpielerNamestr);
    22.           SpielerNamestr = PlayerPrefs.GetString("Spielername");
    23.           SpielerName.text = SpielerNamestr;
    24.  
    25.       }
    26.      
    27.      */
    28. }
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    SpielerN() looks like a convoluted way of setting the text of SpielerName equal to the text of iField.

    (iField.text goes into the variable SpielerNamestr, then SpielerNamestr goes into playerprefs, then you read the value that you just set back out of playerprefs and put it into the same exact variable, then you set SpielerName.text based on that variable.)

    If you are running this from Start(), then I'm guessing iField is going to be empty because the scene just loaded. (But I can't actually tell where you're calling this function, or even if you are calling it at all, since you have commented out the function and have not shown any calls to it.)