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?

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 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 : MonoBehaviour
{
    [Header("Username Player Makes 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!!!