Saving user input as playerprefs

Hey, I’ve been stuck with trying to pull the string from the user input and saving it in my playerprefs as a username. Attached is the code.

    public class Username : MonoBehaviour
    {
        public InputField inputField;
        public Text text;
        //public bool alreadyName;

        void Start()
        {
          

            text.text = PlayerPrefs.GetString ("username");
              

        }

        public void SaveUsername(string username)
        {
          
            text.text = inputField.text;
                PlayerPrefs.SetString ("username", username);

        }


    }

I want the text box to be the user input whenever changing the scene. The problem here is, I can type the user input but as soon I change scenes, the input box is blank as if no playerprefs were loaded.

Oh, I also have the On end Edit checked for the function SaveUsername on my UserInput.

How are you calling SaveUsername? I see you’re setting the text from the inputfield to another text object, but then you’re saving the paramater that you’re passing it.

Make sure your playpref is saving also. with PlayerPrefs.Save();

I put it in my edit, I used OnEndEdit thing on the UserInput

The first thing to do is make sure it’s getting called so put a Debug.Log(“save called”); in the SaveUsername function.

Ok, so, the issue is you aren’t saving what’s from the inputfield. When you set it up in the OnEndEdit, because your username is a parameter, it will show a place where you can type a string. I’m guessing you aren’t typing in a string, which means you have a blank username.

You’re basically saving an empty string.

 PlayerPrefs.SetString ("username", inputField.text);

Is most likely what you want.

2 Likes

I’ll give that a shot, thanks, I’ll come back in 30

And as @fire7side mentioned. Debug.Log is your friend. You should always use it to make sure stuff is being called or you’ve got the correct value. So in this case, you should use it to print out username(since that is what you were saving) and the value of inputField.text.

Ok, so I tried what you said to change username to inputField.text and adding the debug, the debug does appear, but the string does not load

ok did some modding, finally got it :slight_smile: thanks guys!

Glad you got it working :slight_smile:

Thank you!!