I'm making an inventory system for a Unity game.
It saves the inventory just fine, as you can see here: http://ift.tt/1AFCHQz
Here's my save-function:
public void SaveInventoryToXML()
{
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof (List<Item>));
System.IO.StreamWriter file = new System.IO.StreamWriter(inventoryPath);
writer.Serialize(file, inventory);
file.Close();
}
And the load-function:
public void LoadInventoryFromXML()
{
System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(List<Item>));
StreamReader sr = new StreamReader(inventoryPath);
inventory = (List<Item>) reader.Deserialize(sr);
sr.Close();
}
Here's the item class:
using UnityEngine;
using System.Collections.Generic;
using System.Runtime.InteropServices;
[System.Serializable] //what's with the square brackets
public class Item
{
public string ItemName;
public int ItemId;
public string ItemDesc;
public int BaseWorth;
public int BaseDamage;
public int RequiredLevel;
public Sprite Sprite;
public ItemType itemType;
public ItemRarity itemRarity;
public Weapon.WeaponType weaponType;
public Weapon.AttackType attackType;
public List<Enchantment> Enchantments;
public bool Stackable;
public int StackSize = 99;
public int Amount = 1;
public enum ItemType
{
Weapon, Consumable, Quest, Armor, Boots, Helmet, Gloves, Ring, Amulet
}
public enum ItemRarity
{
Common, Rare, Unique, Magic
}
public Item(string name, int id, int reqlvl, string desc, int baseworth, int basedamage, List<Enchantment> enchantments, ItemType itemtype, ItemRarity itemrarity, Sprite sprite )
{
ItemName = name;
ItemId = id;
RequiredLevel = reqlvl;
ItemDesc = desc;
BaseWorth = baseworth;
BaseDamage = basedamage;
Enchantments = enchantments;
itemType = itemtype;
itemRarity = itemrarity;
Sprite = sprite;
additionalParameters();
}
private void additionalParameters()
{
switch (itemType)
{
case ItemType.Consumable:
Stackable = true;
break;
}
}
public Item()
{
ItemId = -1;
}
}
And here's the enchantment class:
using UnityEngine;
using System.Collections;
public class Enchantment
{
public EnchantmentType Type;
public int Amount;
public enum EnchantmentType
{
Fire, Ice, Poison, Electric, Brutalize, Greed, Invisiblity, Heal, ReplenishStamina, ReplenishMana,
AddMaxHealth, AddMaxStamina, AddMaxMana
}
public Enchantment(EnchantmentType type, int amount)
{
Amount = amount;
Type = type;
}
public Enchantment()
{
}
}
All the items are set to null, and I get a NullReferenceException. Here's the enormous error: http://ift.tt/1AFCGMz
What am I doing wrong?
No comments:
Post a Comment