C# How to convert a struct type list into a string?
Hi all, I am doing a game in Unity and need help with converting my struct list into a string. My codes below are put inside a class called, MonsterHandler.
public enum S_STATE
{
S_IDLE,
S_PATROL,
S_ATTACK,
NONE
}
public struct MONSTERS
{
public string Name;
public int Health;
public int Damage;
public S_STATE State;
};
public List<MONSTERS> monsterList= new List<MONSTERS>();
void Start()
{
// add data into the monsterList
// Debug.Log(monsterList.Count); // print out 5
Debug.Log(monsterList.ToArray()); //printed out "MonsterHandler + MONSTERS"
// i want to do something like this
// "Monster_Zombie, 100, 20, S_IDLE, Monster_Donkey, 80, 30, S_IDLE, Monster_Chicken, 120 , 10, IDLE,.."
}
Is there a way to cast my struct list into a string[], then convert into a string? Or there any other ways?
Comment
Answer by unit_nick · Sep 22, 2017 at 04:52 AM
I expect this is enough information for you to work with
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
foreach (var field in typeof(MONSTERS).GetFields(bindingFlags))
{
Debug.Log(field.ToString());
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612135346im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
storing multiple data types in a list 0 Answers
Problem with Fisher Yates Shuffle in C# 0 Answers
Getting objects out of a list and then comparing them 0 Answers
Error in Inventory Script 1 Answer
error with list & struct 1 Answer