I can put the code here but its kind of long and messy so get ready lol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Linq;
public class PlayerManager : MonoBehaviour
{
[SerializeField] private Canvas canvas;
private Player lastMade, tempPlayer;
public GameObject playerMenu;
public TextMeshProUGUI strText, dexText, consText, chaText, wisText, arcmText;
public string characterName, tempName;
public List<Player> Players = new List<Player> {};
public int statsPerLevel = 3;
TextMeshProUGUI playerStrText = null, playerDexText = null, playerConsText = null, playerChaText = null, playerWisText = null, playerArcmText = null, Level = null, StatPoints = null;
public class Player
{
private int str, wis, cons, cha, arcm, dex, lvl, statpoints;
private string name;
public int Str { get { return str; } set { str = value; } }
public int Wis { get { return wis; } set { wis = value; } }
public int Cons { get { return cons; } set { cons = value; } }
public int Cha { get { return cha; } set { cha = value; } }
public int Arcm { get { return arcm; } set { arcm = value; } }
public int Dex { get { return dex; } set { dex = value; } }
public int Lvl { get { return lvl; } set { lvl = value; } }
public int Statpoints { get { return statpoints; } set { statpoints = value; } }
public string Name { get { return name; } set { name = value; } }
public Player(string name, int str, int wis, int cons, int cha, int arcm, int dex, int lvl, int statpoints)
{
this.name = name;
this.str = str;
this.wis = wis;
this.cons = cons;
this.arcm = arcm;
this.dex = dex;
this.lvl = lvl;
this.statpoints = statpoints;
}
}
public void UpdateName(string name) { tempName = name; }
public Player crateCharacter(string name)
{
bool are30 = false;
int statsum = 0;
int[] stats = new int[6];
while (are30 == false)
{
for (int i = 0; i < stats.Length; i++)
{
stats[i] = Random.Range(1, 30);
}
for (int i = 0; i < stats.Length; i++)
{
statsum = statsum + stats[i];
}
if (statsum == 30)
{
are30 = true;
}
statsum = 0;
}
int str = stats[0], wis = stats[1], cons = stats[2], cha = stats[3], arcm = stats[4], dex = stats[5];
Player player = new(name, str, wis, cons, cha, arcm, dex, 0, 0);
player.Cha = stats[3];
return player;
}
public void SetPlayer()
{
Player player = null;
foreach (Player player1 in Players)
{
if (player1.Name == tempName)
{
Debug.Log("Name exists.");
return;
}
}
player = crateCharacter(tempName);
Debug.Log(player.Name);
tempPlayer = player;
strText.text = player.Str.ToString();
dexText.text = player.Dex.ToString();
consText.text = player.Cons.ToString();
chaText.text = player.Cha.ToString();
wisText.text = player.Wis.ToString();
arcmText.text = player.Arcm.ToString();
Debug.Log("Temp player is: "+tempPlayer.Name);
Debug.Log("List length in SetPlayer is: " + Players.Count);
}
public void ConfirmPlayer(GameObject readyPlayerBox)
{
Debug.Log("Temp player in ConfirmPlayer is: " + tempPlayer.Name);
if (tempPlayer == lastMade)
{
return;
}
Vector3 scale = new Vector3(1f, 1f, 1f);
RectTransform originalRectTransform = readyPlayerBox.GetComponent<RectTransform>();
Vector2 newBoxPos = new Vector2(originalRectTransform.anchoredPosition.x + Players.Count * 130 + 130, originalRectTransform.anchoredPosition.y);
GameObject playerBox = Instantiate(readyPlayerBox);
RectTransform newRectTransform = playerBox.GetComponent<RectTransform>();
TextMeshProUGUI playerText = playerBox.GetComponentInChildren<TextMeshProUGUI>();
playerText.rectTransform.SetParent(playerBox.transform.GetChild(0).parent);
playerBox.transform.SetParent(readyPlayerBox.transform.parent);
playerBox.transform.name = tempPlayer.Name + " player button";
newRectTransform.localScale = scale;
playerText.enableWordWrapping = false;
playerText.name = tempPlayer.Name + " Text";
playerText.text = tempPlayer.Name;
playerText.rectTransform.anchoredPosition = new Vector2(0 , 0);
playerText.rectTransform.offsetMax = new Vector2(-5, -5);
playerText.rectTransform.offsetMin = new Vector2(5, 5);
newRectTransform.anchoredPosition = newBoxPos;
Players.Add(tempPlayer);
Debug.Log(Players.Count);
lastMade = Players.Last();
Debug.Log("List length in ConfirmPlayer after setting lastmade is: " + Players.Count);
//Specific player menu making
GameObject MakeSpecificMenu = Instantiate(playerMenu);
RectTransform rt = MakeSpecificMenu.GetComponent<RectTransform>();
MakeSpecificMenu.transform.SetParent(playerMenu.transform.parent);
MakeSpecificMenu.name = tempPlayer.Name + " menu";
rt.position = new Vector2(500, 50);
rt.localScale = new Vector3(1, 1, 1);
//List of stat texts
TextMeshProUGUI[] stats = MakeSpecificMenu.GetComponentsInChildren<TextMeshProUGUI>();
foreach (var stat in stats)
{
if (stat.name == "LevelNum")
{
stat.name = tempPlayer.Name + " LevelNum";
stat.text = tempPlayer.Lvl.ToString();
}
else if (stat.name == "StatPointsNum")
{
stat.name = tempPlayer.Name + " StatPointsNum";
stat.text = tempPlayer.Statpoints.ToString();
}
else if (stat.name == "StrenghNum")
{
stat.name = tempPlayer.Name + " StrenghNum";
stat.text = tempPlayer.Str.ToString();
}
else if (stat.name == "DexterityNum")
{
stat.name = tempPlayer.Name + " DexterityNum";
stat.text = tempPlayer.Dex.ToString();
}
else if (stat.name == "ConstitutionNum")
{
stat.name = tempPlayer.Name + " ConstitutionNum";
stat.text = tempPlayer.Cons.ToString();
}
else if (stat.name == "CharismaNum")
{
stat.name = tempPlayer.Name + " CharismaNum";
stat.text = tempPlayer.Cha.ToString();
}
else if (stat.name == "WisdomNum")
{
stat.name = tempPlayer.Name + " WisdomNum";
stat.text = tempPlayer.Wis.ToString();
}
else if (stat.name == "ArcmanaNum")
{
stat.name = tempPlayer.Name + " ArcmanaNum";
stat.text = tempPlayer.Arcm.ToString();
}
}
}
public void OpenSpecificMenu(GameObject button)
{
PlayerManager currentMenu = null;
PlayerManager[] menus = canvas.GetComponentsInChildren<PlayerManager>();
string playerName = button.GetComponentInChildren<TextMeshProUGUI>().text;
Debug.Log("List length in OpenSpecificMenu is: " + Players.Count);
foreach (var item in menus)
{
Debug.Log(item.name);
}
foreach (var item in menus)
{
Debug.Log(item.name);
if (item.name == playerName)
{
currentMenu = item;
}
}
Debug.Log("Current menu is: " + currentMenu);
currentMenu.SetActive(true);
}
public void UpdateStats(Player currentPlayer)
{
playerStrText.text = currentPlayer.Str.ToString();
playerDexText.text = currentPlayer.Dex.ToString();
playerConsText.text = currentPlayer.Cons.ToString();
playerChaText.text = currentPlayer.Cha.ToString();
playerWisText.text = currentPlayer.Wis.ToString();
playerArcmText.text = currentPlayer.Arcm.ToString();
Level.text = currentPlayer.Lvl.ToString();
StatPoints.text = currentPlayer.Statpoints.ToString();
}
//Start of buttons
//Levels
public void AddLvl(GameObject button)
{
Debug.Log(button.transform.parent.parent.name);
Player currentPlayer = null;
Debug.Log(Players.Count, button);
for (int i = 0; i < Players.Count; i++)
{
Player player = Players[i];
Debug.Log("Player name: " + player.Name);
if (player.Name + " menu" == button.transform.parent.parent.name)
{
currentPlayer = player;
Debug.Log("Set current player to: " + currentPlayer);
}
}
Debug.Log("Current player is: " + currentPlayer);
currentPlayer.Lvl += 1;
currentPlayer.Statpoints += 3;
UpdateStats(currentPlayer);
Debug.Log("Level added");
Debug.Log("player has " + currentPlayer.Statpoints);
}
public void RemoveLvl(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Lvl <= 0)
return;
currentPlayer.Lvl -= 1;
currentPlayer.Statpoints -= 3;
UpdateStats(currentPlayer);
Debug.Log("Level removed");
}
//Strengh
public void AddStr(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Statpoints <= 0) { return; }
currentPlayer.Str += 1;
currentPlayer.Statpoints -= 1;
Debug.Log("Strengh added");
UpdateStats(currentPlayer);
}
public void RemoveStr(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Str <= 0)
return;
currentPlayer.Str -= 1;
currentPlayer.Statpoints += 1;
Debug.Log("Strengh removed");
UpdateStats(currentPlayer);
}
//Dexterity
public void AddDex(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Statpoints <= 0) { return; }
currentPlayer.Dex += 1;
currentPlayer.Statpoints -= 1;
Debug.Log("Dexterity added");
UpdateStats(currentPlayer);
}
public void RemoveDex(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Dex <= 0)
return;
currentPlayer.Dex -= 1;
currentPlayer.Statpoints += 1;
Debug.Log("Dexterity removed");
UpdateStats(currentPlayer);
}
//Constitution
public void AddCons(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Statpoints <= 0) { return; }
currentPlayer.Cons += 1;
currentPlayer.Statpoints -= 1;
Debug.Log("Constitution added");
UpdateStats(currentPlayer);
}
public void RemoveCons(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Cons <= 0)
return;
currentPlayer.Cons -= 1;
currentPlayer.Statpoints += 1;
Debug.Log("Constitution removed");
UpdateStats(currentPlayer);
}
//Charisma
public void AddCha(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Statpoints <= 0) { return; }
currentPlayer.Cha += 1;
currentPlayer.Statpoints -= 1;
Debug.Log("Charisma added");
UpdateStats(currentPlayer);
}
public void RemoveCha(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Cha <= 0)
return;
currentPlayer.Cha -= 1;
currentPlayer.Statpoints += 1;
Debug.Log("Charisma removed");
UpdateStats(currentPlayer);
}
//Wisdom
public void AddWis(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Statpoints <= 0) { return; }
currentPlayer.Wis += 1;
currentPlayer.Statpoints -= 1;
Debug.Log("Wisdom added");
UpdateStats(currentPlayer);
}
public void RemoveWis(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Wis <= 0)
return;
currentPlayer.Wis -= 1;
currentPlayer.Statpoints += 1;
Debug.Log("Wisdom removed");
UpdateStats(currentPlayer);
}
//Arcmana
public void AddArcm(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Statpoints <= 0) { return; }
currentPlayer.Arcm += 1;
currentPlayer.Statpoints -= 1;
Debug.Log("Arcmana added");
UpdateStats(currentPlayer);
}
public void RemoveArcm(GameObject button)
{
Player currentPlayer = null;
foreach (var player in Players)
{
if (player.Name == (button.transform.parent).name)
{
currentPlayer = player;
}
}
if (currentPlayer.Arcm <= 0)
return;
currentPlayer.Arcm -= 1;
currentPlayer.Statpoints += 1;
Debug.Log("Arcmana removed");
Debug.Log("Player has arcmana: " + currentPlayer.Arcm);
UpdateStats(currentPlayer);
}
/// </End of button
public void FixedUpdate()
{
//playerStrText.text = currentPlayer.Str.ToString();
//playerDexText.text = currentPlayer.Dex.ToString();
//playerConsText.text = currentPlayer.Cons.ToString();
//playerChaText.text = currentPlayer.Cha.ToString();
//playerWisText.text = currentPlayer.Wis.ToString();
//playerArcmText.text = currentPlayer.Arcm.ToString();
//Level.text = currentPlayer.Lvl.ToString();
//StatPoints.text = currentPlayer.Statpoints.ToString();
}
}