Save input field


I’m using an inputfield where the user can write his username. But when I change scenes and get back to this scene, the field is empty again. So how can I save the text?

I’m sure it’s easy to do, but I didn’t find how. That’s the last thing I have to do to finish my game, I think I have spent all my energy and my brain is out of order now haha.


(Ps: Sorry for my bad English I’m french)

When you switch scene all objects (apart from ones with don’t destroy on load enabled) are destroyed, so your input is lost. One way to save your input would be to save the username to a static variable in some script, then when you enter the scene with the input field you load that data from the script again. Or you could write the data to a file on the computer / use playerprefs if you wanted it to remember the username between launches of the game. Heres an example of how you might do it:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MenuControl : MonoBehaviour {
    public InputField usernameInput;
    public static string username;

    void Start() {
        if(username != null)
            usernameInput.text = username;

    public void SaveUsername(string newName) {
        username = newName;

Then just add the referecne to the inputfield and add the “SaveUsername” method to the end edit event of the inputfield, and jobs a goodun. Then if you want it to persist through launches of the game you could change SaveUsername to save data, and start to load that data.

Thanks for your help, It works now. if someone have the same issue here is what I’ve done:

public class nomScript : MonoBehaviour {
         public InputField usernameInput;
         public bool alreadyName;

void Start () {
	if (alreadyName = true) {
		usernameInput.text = PlayerPrefs.GetString ("saveNom");				
public void SaveUsername(string newName) {
	alreadyName = true;
	PlayerPrefs.SetString ("saveNom", newName); } }

I’ve used a bollean (alreadyName) to detect if the user has already wrote in the inputfield before. If it’s true it shows text saved with PlayerPref.
To save the text in the PlayerPref I have added the “SaveUsername” method to the end edit event of the inputfield.

What you want is to persist some info, since objects are destroyed when moving to a different scene (unless marked to not being destroyed). The simplest way is to use PlayerPrefs to store the value when you leave the scene, and to read the value again un some Start method when you load de scene.

With this method the field will be filled again the next time you run your game. There are more methods similar to this that consist on saving what you want into a file instead of playerprefs.

Another option is to create some class with a static member to “store” the name during runtime. The static member will keep it’s value when changing the scene, but the next time you run your game it will be gone.

A good way to store game/player data is in a JSON string, then save it as a binary file. This option would be a lot more secure than playerprefs and you can store a lot of data in a JSON string e.g name, score, level, lastPos, ammo, time etc.

Then you could take your input and do something like…

void CreatePlayerData()
//Create JSON string with default values e.g lives = 3, score = 0, level = 1

//Save string to file


void GetPlayerData()
//Return json string from file

then you can update or parse the json string…

void SetScore()
//set score to json string and save to file

void GetScore()
//Get json string from file using GetPlayerData();
//Parse json string and retreive score
//return score

Setting up JSON and saving to file can be a bit long, so I haven’t gone into much detail, but there is plenty of information out there on both subjects.

Unity provides a great video tutorial on Persisting Data / Saving And Loading, you can find it here…

This should cover the saving as file part and also playerprefs.

The following link is for JSON…
SimpleJSON is an easy to use JSON parser, there is lots of info about it available.

I hope this helps you in some way, and sorry I haven’t gone into massive detail.

p.s. I’m not an expert, so apologies if I’ve missed anything