我正在創建一個Loot系統。我幾乎快結束了,只剩下DropTable在Enemy腳本中的Inspector中填寫了。由于某種原因,我的DropTable腳本正在序列化,但是我的LootDrop班級卻沒有。我的班級基本上是這樣設置的:DropTable 班級:[System.Serializable]public class DropTable{ public List<LootDrop> loot = new List<LootDrop>(); public Item GetDrop() { int roll = Random.Range(0, 101); int chanceSum = 0; foreach (LootDrop drop in loot) { chanceSum += drop.Chance; if (roll < chanceSum) { return ItemDatabase.Instance.GetItem(drop.itemSlug); //return the item here } } return null; //Didn't get anything from the roll }}LootDrop 班級:[System.Serializable]public class LootDrop{ public string itemSlug { get; set; } public int Chance { get; set; }}本質上,我DropTable只包含的列表LootDrop。但是,我無法從檢查器訪問其中的各個LootDrop實例List<LootDrop>。我要做的就是public DropTable在Enemy腳本上創建一個變量。我覺得我以前做過類似的事情,沒有問題。我在這里做錯什么了嗎?我真的很想DropTable成為與我的敵人不同的班級,因為敵人不應該真正在乎該GetDrop()方法。但是,如果那是唯一的方法,那么我想它必須這樣做。在這個問題上的任何幫助將不勝感激。
2 回答

Helenr
TA貢獻1780條經驗 獲得超4個贊
Unity將序列化字段,而不是屬性??梢郧袚Q到以下字段:
[Serializable]
public class LootDrop
{
public int Chance;
}
或使用序列化的后備字段:
[Serializable]
public class LootDrop
{
public int Chance
{
get { return _chance; }
set { _chance = value; }
}
[SerializeField]
private int _chance;
}

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
您應loot在嘗試添加項目之前初始化變量。
[System.Serializable]
public class DropTable
{
public List<LootDrop> Loot;
public DropTable()
{
Loot = new List<LootDrop>();
}
}
另外,請謹慎使用名稱約定。
- 2 回答
- 0 關注
- 292 瀏覽
添加回答
舉報
0/150
提交
取消