Hello,
I’m attempting to create a character stats screen for a tactical RPG type game where you can look through all of your units and see their stats in one page.
I have the roster of the characters as below:
public class Roster : MonoBehaviour {
public int characters;
public BaseCharacter[] characterRoster;
void Start () {
characterRoster = new BaseCharacter[characters];
for (int i = 0; i < characters; i++)
{
characterRoster *= new BaseCharacter();*
//defining stats
characterRoster*.characterName = “name:” + i.ToString();*
}
* }*
}
and I’d like to make it so that a panel gets a child panel for each character in the roster with the various character variables I have and will be adding in:
public class BaseCharacter : MonoBehaviour {
public string characterName;
}
If anyone could help, I’d greatly appreciate it. Thank you for your time.
First you cannot do new BaseCharacter(); because BaseCharacter is a MonoBehaviour class which is not allowed.
So you will either need to create a separate character data class which is used by the BaseCharacter class or make the BaseCharacter class a generic class.
You can however do this
public class CharacterData
{
public string characterName;
// add any other character stats here
// e.g.
public int strength;
public int dexterity;
public int intelligence;
public int resistance;
public int vitality;
public int criticalHitChance;
public int criticalHitDamage;
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Roster : MonoBehaviour
{
public int characters;
public List<CharacterData> characterRoster = new List<CharacterData>();
public GameObject parentCanvas;
public GameObject characterPanelPrefab;
void Start ()
{
if (parentCanvas == null)
{
Debug.LogError("parentCanvas is not set in the inspector. Please set before continuing");
}
if (characterPanelPrefab == null)
{
Debug.LogError("characterPanelPrefab is not set in the inspector. Please set before continuing");
}
for (int i = 0; i < characters; i++)
{
CharacterData character = new CharacterData();
character.characterName = "name:" + i.ToString();
// add any other character stats here
// e.g.
strength = (int)Random.Range(20, 50);
dexterity = (int)Random.Range(20, 50);
intelligence = (int)Random.Range(20, 50);
resistance = (int)Random.Range(20, 50);
vitality = (int)Random.Range(20, 50);
criticalHitChance = (int)Random.Range(20, 40);
criticalHitDamage = (int)Random.Range(20, 40);
characterRoster.Add(character);
AddCharacterPanel(character);
}
}
void AddCharacterPanel(CharacterData character)
{
GameObject spawnedObj = Instantiate(characterPanelPrefab);
// make the spawned panel a child of the parentCanvas
spawnedObj.transform.SetParent(parentCanvas.transform, false);
// you need to do this for each item in your CharacterData class that you want on the panel
// e.g.
// get a GameObject named "Name" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("Name"))
{
spawnedObj.transform.Find("Name").GetComponent<Text>() = character.characterName;
}
// get a GameObject named "Strength" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("Strength"))
{
spawnedObj.transform.Find("Strength").GetComponent<Text>() = character.strength;
}
// get a GameObject named "Dexterity" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("Dexterity"))
{
spawnedObj.transform.Find("Dexterity").GetComponent<Text>() = character.dexterity;
}
// get a GameObject named "Intelligence" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("Intelligence"))
{
spawnedObj.transform.Find("Intelligence").GetComponent<Text>() = character.intelligence;
}
// get a GameObject named "Resistance" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("Resistance"))
{
spawnedObj.transform.Find("Resistance").GetComponent<Text>() = character.resistance;
}
// get a GameObject named "Vitality" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("Vitality"))
{
spawnedObj.transform.Find("Vitality").GetComponent<Text>() = character.vitality;
}
// get a GameObject named "CriticalHitChance" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("CriticalHitChance"))
{
spawnedObj.transform.Find("CriticalHitChance").GetComponent<Text>() = character.criticalHitChance;
}
// get a GameObject named "CriticalHitDamage" under the spawned prefab GameObject (must have a Text component attached)
if (spawnedObj.transform.Find("CriticalHitDamage"))
{
spawnedObj.transform.Find("CriticalHitDamage").GetComponent<Text>() = character.criticalHitDamage;
}
}
}
Depending on your panel structure tou will need to create a UI prefab something like the one listed below (based on the data above)
UI Panel Object
UI Text Object - name: "Name"
UI Text Object - name: "Strength"
UI Text Object - name: "Dexterity"
UI Text Object - name: "Intelligence"
UI Text Object - name: "Resistance"
UI Text Object - name: "Vitality"
UI Text Object - name: "CriticalHitChance"
UI Text Object - name: "CriticalHitDamage"