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.