- Home /
Saving username using InputField and PlayerPrefs
I am trying to make PlayerPrefs save more than one registered user & display their logged in name on the game screen. I know how to do it for ONE player, but when I tried logging for a DIFFERENT player it only displayed the last registered users name, health, and gold amounts.
This is as far as I have gotten on the Registration script ( to keep track of my old one from the new one I named it "TestUserRegistration").
using UnityEngine;
using UnityEngine.UI;
public class TestUserRegistration : MonoBehaviour
{
public InputField username_textBox;
public InputField password_textBox;
public int numUsers;
public string[] userNames;
public void Start()
{
numUsers = PlayerPrefs.GetInt("NumUsers", 0); //how many registered users?
userNames = new string[numUsers]; //create the user name array...
for (var n=1; n <= numUsers; n++)
{
userNames[n] = PlayerPrefs.GetString("User" + n, ""); //and load them.
}
}
public void Saving()
{
PlayerPrefs.SetString("User" + numUsers.ToString(), newUserName);
numUsers++;
PlayerPrefs.SetInt("NumUsers", numUsers);
}
}
I am receiving an error in Visual Studio about this line of code in the Saving.
PlayerPrefs.SetString("User" + numUsers.ToString(), newUserName);
The error says, "The name 'newUserName' does not exist in the current context". I'm not understanding WHERE I would put a 'newUserName'. Could someone PLEASE help me?
Answer by Hellium · Apr 17, 2019 at 07:11 PM
using UnityEngine;
using UnityEngine.UI;
public class TestUserRegistration : MonoBehaviour
{
public InputField Username_textBox;
public InputField Password_textBox;
private int usersCount;
private string[] userNames;
public void Start()
{
// Retrieve the number of registered users
usersCount = PlayerPrefs.GetInt("UsersCount", 0);
if( usersCount > 0 )
{
// Create the user name array...
userNames = new string[usersCount];
for( int index = 0 ; index < usersCount ; index++)
{
// ... and load them.
userNames[index] = PlayerPrefs.GetString("User" + index, string.Empty );
}
}
}
public void SaveUserName()
{
PlayerPrefs.SetString("User" + usersCount, Username_textBox.text);
usersCount++;
PlayerPrefs.SetInt("UsersCount", usersCount);
}
}
@Hellium Your script is pushing more errors than $$anonymous$$e is.
I've fixed the single error I got.
userNames[index] = PlayerPrefs.GetString("User" + n, string.Empty );
should be
userNames[index] = PlayerPrefs.GetString("User" + index, string.Empty );
@Hellium Sorry it took so long for me to respond, busy week. I tested the script & it seems to be saving just fine. Thank you so very much! It is greatly appreciated!
BUT how do I display the username on the main game screen in my own textbox? I do not like using void onGUI premade boxes. I would like to make my own text box. This is the script I have so far but it will not let me even test due to an error on the last line. It is saying "the name index does not exist in the current context".
using UnityEngine;
using UnityEngine.UI;
public class DisplayingPlayerName : $$anonymous$$onoBehaviour
{
[Header("Username Player $$anonymous$$akes Box")]
public InputField username_textBox;
private int usersCount;
private string[] userNames;
public void Start()
{
// Retrieve the number of registered users.
usersCount = PlayerPrefs.GetInt("UsersCount", 0);
if (usersCount > 0)
{
// Create the user name array...
userNames = new string[usersCount];
for (int index = 0; index < usersCount; index++)
{
//...and load them.
userNames[index] = PlayerPrefs.GetString("User" + index, string.Empty);
}
}
}
// This reads from PlayerPrefs the LAST registered user ONLY.
public void Update()
{
username_textBox = PlayerPrefs.GetString("User" + index, string.Empty);
}
}
Could you PLEASE help!!!!
I don't understand what you want to do exactly. Do you want to display the list of saved users on the screen with multiple text boxes (or the last saved one)? Or do you want to prefill the inputext with the last saved user?
@Hellium I'm trying to set up a register/login system using PlayerPrefs. I have a "Pick your color" panel when the game 1st loads. Each different color button is attached to a SlotID. Then the register panel opens up when they click save. They create their username & password, click save.
On the login panel, I want a duplicate panel of the "Pick your color" panel to show with all the player's names on the color they picked. They are suppose to be able to just click on their name/color box & the PlayerPrefs is suppose to load all that players info.
On the main screen I'm trying to display the Player's choosen name, but that isn't working. I'm also having a hard time figuring out how to tell PlayersPrefs which color button is selected.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I set a player tag to this line of code? 1 Answer