Tengo un script del asset Inventory Master lo he modificado para el juego que estoy desarrollando. Es el siguiente:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class PlayerInventory : MonoBehaviour
{
public GameObject inventory;
public GameObject characterSystem;
public GameObject craftSystem;
private Inventory craftSystemInventory;
private CraftSystem cS;
private Inventory mainInventory;
private Inventory characterSystemInventory;
private Tooltip toolTip;
private InputManager inputManagerDatabase;
public GameObject HPMANACanvas;
public Text hpText;
public Text hambreText;
public Text sedText;
public Image hpImage;
public Image hambreImage;
public Image sedImage;
float maxHealth = 100;
float maxHambre = 100;
float maxSed = 100;
public float currentHealth = 100;
public float currentHambre = 100;
public float currentSed = 100;
int normalSize = 3;
public void OnEnable()
{
Inventory.ItemEquip += OnBackpack;
Inventory.UnEquipItem += UnEquipBackpack;
Inventory.ItemEquip += OnGearItem;
Inventory.ItemConsumed += OnConsumeItem;
Inventory.UnEquipItem += OnUnEquipItem;
Inventory.ItemEquip += EquipWeapon;
Inventory.UnEquipItem += UnEquipWeapon;
}
public void OnDisable()
{
Inventory.ItemEquip -= OnBackpack;
Inventory.UnEquipItem -= UnEquipBackpack;
Inventory.ItemEquip -= OnGearItem;
Inventory.ItemConsumed -= OnConsumeItem;
Inventory.UnEquipItem -= OnUnEquipItem;
Inventory.UnEquipItem -= UnEquipWeapon;
Inventory.ItemEquip -= EquipWeapon;
}
void EquipWeapon(Item item)
{
if (item.itemType == ItemType.Weapon)
{
//add the weapon if you unequip the weapon
}
}
void UnEquipWeapon(Item item)
{
if (item.itemType == ItemType.Weapon)
{
//delete the weapon if you unequip the weapon
}
}
void OnBackpack(Item item)
{
if (item.itemType == ItemType.Backpack)
{
for (int i = 0; i < item.itemAttributes.Count; i++)
{
if (mainInventory == null)
mainInventory = inventory.GetComponent<Inventory>();
mainInventory.sortItems();
if (item.itemAttributes[i].attributeName == "Slots")
changeInventorySize(item.itemAttributes[i].attributeValue);
}
}
}
void UnEquipBackpack(Item item)
{
if (item.itemType == ItemType.Backpack)
changeInventorySize(normalSize);
}
void changeInventorySize(int size)
{
dropTheRestItems(size);
if (mainInventory == null)
mainInventory = inventory.GetComponent<Inventory>();
if (size == 3)
{
mainInventory.width = 3;
mainInventory.height = 1;
mainInventory.updateSlotAmount();
mainInventory.adjustInventorySize();
}
if (size == 6)
{
mainInventory.width = 3;
mainInventory.height = 2;
mainInventory.updateSlotAmount();
mainInventory.adjustInventorySize();
}
else if (size == 12)
{
mainInventory.width = 4;
mainInventory.height = 3;
mainInventory.updateSlotAmount();
mainInventory.adjustInventorySize();
}
else if (size == 16)
{
mainInventory.width = 4;
mainInventory.height = 4;
mainInventory.updateSlotAmount();
mainInventory.adjustInventorySize();
}
else if (size == 24)
{
mainInventory.width = 6;
mainInventory.height = 4;
mainInventory.updateSlotAmount();
mainInventory.adjustInventorySize();
}
}
void dropTheRestItems(int size)
{
if (size < mainInventory.ItemsInInventory.Count)
{
for (int i = size; i < mainInventory.ItemsInInventory.Count; i++)
{
GameObject dropItem = (GameObject)Instantiate(mainInventory.ItemsInInventory[i].itemModel);
dropItem.AddComponent<PickUpItem>();
dropItem.GetComponent<PickUpItem>().item = mainInventory.ItemsInInventory[i];
dropItem.transform.localPosition = GameObject.FindGameObjectWithTag("Player").transform.localPosition;
}
}
}
void Start()
{
if (HPMANACanvas != null)
{
hpText = HPMANACanvas.transform.GetChild(1).GetChild(0).GetComponent<Text>();
hambreText = HPMANACanvas.transform.GetChild(2).GetChild(0).GetComponent<Text>();
sedText = HPMANACanvas.transform.GetChild(3).GetChild(0).GetComponent<Text>();
hpImage = HPMANACanvas.transform.GetChild(1).GetComponent<Image>();
hambreImage = HPMANACanvas.transform.GetChild(1).GetComponent<Image>();
sedImage = HPMANACanvas.transform.GetChild(1).GetComponent<Image>();
UpdateSedBar();
UpdateHPBar();
UpdateHambreBar();
}
if (inputManagerDatabase == null)
inputManagerDatabase = (InputManager)Resources.Load("InputManager");
if (craftSystem != null)
cS = craftSystem.GetComponent<CraftSystem>();
if (GameObject.FindGameObjectWithTag("Tooltip") != null)
toolTip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent<Tooltip>();
if (inventory != null)
mainInventory = inventory.GetComponent<Inventory>();
if (characterSystem != null)
characterSystemInventory = characterSystem.GetComponent<Inventory>();
if (craftSystem != null)
craftSystemInventory = craftSystem.GetComponent<Inventory>();
}
void UpdateHPBar()
{
hpText.text = (currentHealth + "/" + maxHealth);
float fillAmount = currentHealth / maxHealth;
hpImage.fillAmount = fillAmount;
}
void UpdateHambreBar()
{
hambreText.text = (currentHambre + "/" + maxHambre);
float fillAmount = currentHambre / maxHambre;
hambreImage.fillAmount = fillAmount;
}
void UpdateSedBar()
{
sedText.text = (currentSed + "/" + maxSed);
float fillAmount = currentSed / maxSed;
sedImage.fillAmount = fillAmount;
}
public void OnConsumeItem(Item item)
{
for (int i = 0; i < item.itemAttributes.Count; i++)
{
if (item.itemAttributes[i].attributeName == "Health")
{
if ((currentHealth + item.itemAttributes[i].attributeValue) > maxHealth)
currentHealth = maxHealth;
else
currentHealth += item.itemAttributes[i].attributeValue;
}
if (item.itemAttributes[i].attributeName == "Hambre")
{
if ((currentHambre + item.itemAttributes[i].attributeValue) > maxHambre)
currentHambre = maxHambre;
else
currentHambre += item.itemAttributes[i].attributeValue;
}
if (item.itemAttributes[i].attributeName == "Sed")
{
if ((currentSed + item.itemAttributes[i].attributeValue) > maxSed)
currentSed = maxSed;
else
currentSed += item.itemAttributes[i].attributeValue;
}
}
if (HPMANACanvas != null)
{
UpdateSedBar();
UpdateHPBar();
UpdateHambreBar();
}
}
public void OnGearItem(Item item)
{
for (int i = 0; i < item.itemAttributes.Count; i++)
{
if (item.itemAttributes[i].attributeName == "Health")
maxHealth += item.itemAttributes[i].attributeValue;
if (item.itemAttributes[i].attributeName == "Hambre")
maxHambre += item.itemAttributes[i].attributeValue;
if (item.itemAttributes[i].attributeName == "Sed")
maxSed += item.itemAttributes[i].attributeValue;
}
if (HPMANACanvas != null)
{
UpdateSedBar();
UpdateHPBar();
UpdateHambreBar();
}
}
public void OnUnEquipItem(Item item)
{
for (int i = 0; i < item.itemAttributes.Count; i++)
{
if (item.itemAttributes[i].attributeName == "Health")
maxHealth -= item.itemAttributes[i].attributeValue;
if (item.itemAttributes[i].attributeName == "Hambre")
maxHambre -= item.itemAttributes[i].attributeValue;
if (item.itemAttributes[i].attributeName == "Sed")
maxSed -= item.itemAttributes[i].attributeValue;
}
if (HPMANACanvas != null)
{
UpdateHambreBar();
UpdateHPBar();
UpdateSedBar();
}
}
Y cuando le doy al play la consola me dice:
NullReferenceException: Object reference not set to an instance of an object
PlayerInventory.UpdateSedBar () (at Assets/InventoryMaster/Scripts/ExampleScript/PlayerInventory.cs:210)
PlayerInventory.Start () (at Assets/InventoryMaster/Scripts/ExampleScript/PlayerInventory.cs:174)
Alguna idea de porque pasa esto?