How do I make a variable with a name that is data (possibly the contents of a seperate string variable).

I’m looking to make a new Player from a Player class

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  
using UnityEngine.UI;  
  
public class ScriptName : MonoBehaviour {  
  
    public class Player  
    {  
        public string Name;  
        public int Type;  
        public Relation Relation;  
        public int initiative;  
    }  
  
    public Text nameText;        
    public List<Player> Players = new List<Player>();        
    public string playerNumber;        
    public string playerName;        
    public bool finished;        
    public bool naming;        
    public bool enterData;        
    public bool enteredC;        
    public bool buttonPressed;        
  
    void Update()  
    {  
        if (naming == true)  
        {  
            foreach (char c in Input.inputString)  
            {  
                if (c == '\b')  
                {  
                    if (playerName.Length != 0)  
                    {  
                        playerName = playerName.Substring(0, playerName.Length - 1);  
                    }  
                }  
                else if ((c == '

') || (c == ‘\r’))
{
naming = false;
enterData = true;
Player [Players.Count.ToString()] = new Player(); //This is the part I need help with.

                }  
                else  
                {  
                    playerName += c;  
                    if (enteredC == false)  
                    {  
                        enteredC = true;  
                    }  
                }  
            }  
            if (enteredC == true)  
            {  
                nameText.text = playerName;  
            }  
        }  
    }  
}

Every new player added will be named differently, maybe there is some other way to do this that I’m not thinking of.

It’s a button that when pressed will activate this bit of code (that’s where the naming variable comes into play). Afterwards there is more character creation and all but that Player class with the unique name will be added to the Players list. Next time the button is pressed, since there is one more player in the list, the name will still be unique this time.
Maybe I can turn playerNumber into Players.Count.ToString(); with
playerNumber = Players.Count.ToString(); upon the button being pressed, so the unique name for the variable is stored and updated in that manner. If so how would I use the string playerNumber to name a new class.

Player [playerNumber] = new Player(); //This is the part I need help? Because Player playerNumber = new Player(); //This is the part I need help just makes a new Player named playerNumber. That’s of no use.

You cannot make up variable names at runtime. The way to go about this as you have correctly thought of is to make a Player class with a name string variable. There are 2 ways to do what you are trying to do after you declare a new Player instance using Player [playerNumber] = new Player();:

  1. assign the variables under the player class separately

    //this is your list of players
    public List Players = new List();

    //create a new Player class instance
    Player newPlayer = new Player();
    //assign variables separately
    newPlayer.Name = playerName;
    newPlayer.Type = something;
    //etc etc
    //add the new player to the list of players
    Players.Add(newPlayer);

2)make a constuctor function for creating new Player instances (this however works best if you already know all the Player attributes when creating it)

public class Player 
{ 
	public string Name; 
	public int Type; 
	public Relation Relation; 
	public int initiative; 
	
	//constructor below
	public Player(string name, int type, Relation relation, int initiative)	//you can have optional variables here
	{
		this.Name = name;
		this.Type = type;
		this.Relation = relation;
		this.initiative = initiative;
	}
}

//example values of a new player
string name = "Player";
int type = 0;
Relation relation = new Relation();	//you need to set this up
int initiative = 0;
//create and add a new player instance using the constructor
Players.Add(new Player(name, type, relation, initiative));

You can learn more about constructors here.