Panel with Values From Array
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[i] = new BaseCharacter();
//defining stats
characterRoster[i].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.
Answer by TBruce · Jun 05, 2016 at 07:30 PM
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"
Thank you, I appreciate the response! I wound up having something similar to what you outlined just working through it myself; having a function to instantiate prefabs and parent them under my roster panel in the scene. It's reassuring!
I was a little concerned about Unity throwing warnings about how I wasn't able to use the new keyword for $$anonymous$$onobehaviour derived scripts, but it worked so I sort of just ignored the warning. Should I have my BaseCharacter class derive from some CharacterData class? I attempted this and got a bunch of errors saying that the stats did not exist.
I haven't worked much with lists (just some pathfinding tutorials using them for node grids), but I'm quite familiar with arrays. Would using a list to store the roster be more advantageous than using arrays?
Yes Unity will run even when you do a new on a $$anonymous$$onobehaviour script but this is not allowed because $$anonymous$$onobehaviour scripts have to be attached to GameComponents.
Basically as I outlined above, and for simplicity, you only need one prefab for your panel. And on that prefab have several child GameOIbjects to meet your needs.
Lists are basically the same as arrays but Lists have more functionality and are preferred in C#. But to answer your question I would personally say yes especially since resizing an array is more difficult than a list is. To remove an item from a list it is easy as
myList.RemoveAt(n);
where it is not so simple with an array.
Please click the tick to accept the answer if your question was answered.
I'll go ahead and try setting up the roster with a list then, that does seem a lot easier to work with. Also, it's really encouraging when you're new to Unity and game development in general when people help with questions, so thanks again for that.