【发布时间】:2021-02-23 02:09:37
【问题描述】:
错误在这一行。:
if (pItem.isStackable)
{
}
我应该注意,该错误仅在我构建游戏时发生。在编辑器中运行游戏时不会出现这种情况。
我认为错误来自试图在玩家的物品栏中为玩家提供武器。
因为它运行良好:
Player.playerInv.AddItem(apple);
这不是:
Player.playerInv.AddItem(sword2);
唯一的区别是苹果是一件物品,而 Sword2 是一件武器……我有理由相信构建期望加载的是物品而不是武器。 (Weapon 是 item 的子项。item 继承 ScriptableObject)。
这是武器和物品的代码(抱歉,文件太大):
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Item", menuName = "StrangeEngine/Item", order = 2)]
public class Item : ScriptableObject
{
public enum ItemType
{
Item = 1,
Skill = 2,
Equipment = 3
}
public ItemType type;
public int ID;
public string itemName;
public string info;
public int worth;
public bool isStackable = false;
//public Sprite icon;
public Dictionary<string, int> itemStats = new Dictionary<string, int>(); // "stats" that come in pairs, a name (string) and a number. for example ("weight", 22)
public Item(ItemType pType, int pID, string pName, string pInfo, int pWorth, Dictionary<string,int> pStats)
{
type = pType;
ID = pID;
itemName = pName;
info = pInfo;
worth = pWorth;
itemStats = pStats;
}
public Item(ItemType pType, int pID, string pName, string pInfo, int pWorth, Dictionary<string, int> pStats, bool pIsStackable)
{
type = pType;
ID = pID;
itemName = pName;
info = pInfo;
worth = pWorth;
itemStats = pStats;
isStackable = pIsStackable;
}
}
[CreateAssetMenu(fileName = "Item", menuName = "StrangeEngine/Weapon", order = 3)]
public class Weapon : Item
{
public int Damage;
public GameObject WeaponModelPrefab;
public Weapon(int pID, string pName, string pInfo, int pWorth, Dictionary<string, int> pStatsfloat, int pDamage, GameObject pWeaponPrefab) : base(ItemType.Equipment,pID, pName, pInfo,pWorth,pStatsfloat)
{
Damage = pDamage;
WeaponModelPrefab = pWeaponPrefab;
}
}
【问题讨论】:
标签: unity3d serialization build-error scriptable-object