a list with a constructor class that also contains a list.
So I am working on an rpg item database for a pet project that I'm working on. I have run into a snag, and am quite confused as to why something I'm trying to do is not working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Item
{
public string name;
public JobGrades.Grade grade;
public int cost;
public Type type;
public Slot slot;
//this list is not showing up in the unity inspector and I might go bald
//if I don't figure this out soon, please send help and more caffine!!!
//It keeps being treated as if it's not there, even when I try to access
//the information after adding an item to the list in code.
public List<Stat> AffectedStats = new List<Stat>();
public enum Slot
{
None,
Head,
Pauldrons,
Gauntlets,
Chest,
Leggings,
Boots,
OffHand,
MainHand,
TwoHand,
Acc
}
public enum MaterialType
{
Wood,
Stone,
Metal,
Bone,
Exotic,
Precious,
Herb,
Gross
}
public enum Type
{
Equip,
Use,
Material
}
//first constructor. ATM for usables.
public Item(string n, JobGrades.Grade g, Type t, Stat.St[] s, float[] amount, int v)
{
name = n;
grade = g;
type = t;
cost = v;
PopStatsList(s, amount);
}
//second constructor. For equipables.
public Item(string n, JobGrades.Grade g, Type t, Slot sl, Stat.St[] s, float[] amount, int v)
{
name = n;
grade = g;
type = t;
cost = v;
slot = sl;
PopStatsList(s, amount);
}
void PopStatsList(Stat.St[] st, float[] amounts)
{
int i = 0;
foreach (Stat.St s in st)
{
AffectedStats.Add(new Stat(s, amounts[i]));
i++;
}
}
}
any help at this point would be appreciated. Thank you all in advance.
p.s. I have found that I can just have 2 array's of enum.statname, and float array of the amount the stats change, but if I mess up the index numbers even a little.... it's why I want to use the list, lists feel more natural to me than "public float[] nums;"
for reference the Stat script:
public class Stat
{
public St stat;
public float amount;
public enum St
{
HP,
HPMax,
Strength,
Vitality,
Dexterity,
Intelligence,
Will,
PDamage,
MDamage,
PDef,
MDef,
FireResist,
EarthResist,
WindResist,
WaterResist,
EXP,
na
}
public Stat(St st, float change)
{
stat = st;
amount = change;
}
}
what am I missing here? knowing me it's probably something simple, or I'm just stubborn and things do not work like this any way.
Answer by alankemp · May 11, 2017 at 02:01 PM
Unity can't serialise your Stat class, so it isn't showing in the properties and letting you edit it.
Add [Serializable] before the Stat class definition.
[Serializable]
public class Stat
{
public St stat;
//... etc
Your answer
Follow this Question
Related Questions
List give a count of zero when it have 1 element 1 Answer
Getting "object reference not set" error when creating a list dictionary. 1 Answer
Insert string into empty list at a specific index 0 Answers
Adding a unique element from one list to another list 2 Answers
How to remove an item from a list of custom variables 1 Answer