How to save InputField value?

I’m currently working on a game and I’m using the new 4.6 UI InputField for the players to set their username and for it to save. How exactly would I go about doing that? I’m using C# and this is my code so far… It doesn’t work.

CharText is the Text Component that changes when you type from the InputField and charName is the string that is taken from the text which is then saved into PlayerPrefs.

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

public class NetworkManager : MonoBehaviour 
{
	public Text charText;
	private string charName;
	
	void Awake()
	{
		PlayerPrefs.GetString ("Character_Name");
	}
	void Update()
	{
		charText.text = charName;
	}
		
	public void CharName()
	{
		PlayerPrefs.SetString ("Character_Name", charName);
	}
}

GetString returns type of string… so in your Awake function/method, your not passing the value back if “Character_Name” was found in the PlayerPrefs. So it would be null techincally. GetString can also take a second string parameter/argument that is the default if the named key isn’t found.

Here is your example updated:

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

public class NetworkManager : MonoBehaviour
{
	public Text charText;
	private string charName;
	
	void Awake()
	{
		charName = PlayerPrefs.GetString ("Character_Name"); // If the value is found, it is passed back to charName.  Should this go in the Start method?
		// or with a default value if the key Character_Name can not be found
		// charName = PlayerPrefs.GetString ("Character_Name", "NoName"); 
	}
	
	void Update()
	{
		charText.text = charName; // this seems silly, you should put this in start?
	}

	public void CharName()
	{
		PlayerPrefs.SetString ("Character_Name", charName);
	}
}

I don’t know what the method CharName() is doing other then setting the character name key, is this wired up to the text control? Are you letting unity save the playerprefs on application quit or do you need to call:

PlayerPrefs.Save();