- Home /
Getting playername and displaying it with score on gameover scene
i'm a unity newbie. I ve added an input field to the Mainmenu and on the OnValueChanged I ve assigned SetUsername. It does not work the way i want it to.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public string userName;
public UnityEngine.UI.InputField inputField;
public void Awake()
{
userName = PlayerPrefs.GetString("playerName", "Player");
inputField.text = userName;
}
public void SetUserName(string text)
{
userName = text;
PlayerPrefs.SetString("playerName", userName);
}
public void Playgame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void Quitgame()
{
Debug.Log("Quit");
Application.Quit();
}
the code is fine, you would need to share a video of what happens for us to help you
Answer by CobbledGames · Mar 23, 2020 at 04:40 PM
From what I can see SetUserName is not being run because you are not telling the SetUserName to run. You want to put this in a function like Awake:
SetUserName(userName);
This will tell the game to run SetUserName immediately on startup with the userName Variable from your global variables at the top.
He said that he is calling setusername when the inputfield changes, if he calls ir in the awake or in the startup the use of playerprefs would lake no sense because he would be overriding the playerprefs value every game and it would not persist
Ohh. Then I say I don't know then. I think we need to establish exactly what they are intending to do with this menu.