I don’t even know how to formulate a question since I have 0 idea why this error is occurring. My observation, after 5 quadrillion Debug.Log statements, is that somewhere along storing the class array which includes class, the class value becomes null. I have checked this through confirming whether or not the values actually change. It’s better explained through code.
Inventory.cs: handles inventory storage as well as GUI display.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
public ItemClass holdingNow;
inventoryItems[] inventoryStuff = new inventoryItems[5];
public GameObject[] itemIconGUI = new GameObject[5];
public GameObject[] itemNumberGUI = new GameObject[5];
// Use this for initialization
void Start () {
Debug.Log ("start");
inventoryStuff [0] = new inventoryItems (null, "", 0);
inventoryStuff [1] = new inventoryItems (null, "", 0);
inventoryStuff [2] = new inventoryItems (null, "", 0);
inventoryStuff [3] = new inventoryItems (null, "", 0);
inventoryStuff [4] = new inventoryItems (null, "", 0);
}
// Update is called once per frame
void Update () {
//update gui display
for (int i = 0; i < 5; i++) {
Debug.Log ("updating GUI");
itemIconGUI _.GetComponent<Image> ().sprite = Sprite.Create (inventoryStuff *.iClass.itemTexture, new Rect (0, 0, 30, 30), new Vector2 (0.5f, 0.5f));*_
itemNumberGUI .GetComponent ().text = inventoryStuff .numberOfItem.ToString ();
* }*
* }*
* public void AddItem (ItemClass itemClass) {*
* for (int i = 0; i <= 4; i++) {*
Debug.Log (inventoryStuff .iClass + ", " + inventoryStuff .item + ", " + inventoryStuff .numberOfItem);
* }*
* //already exists and stackable*
* for (int i = 0; i <= 4; i++) {*
_ if (inventoryStuff .item == itemClass.itemName) {
* Debug.Log (itemClass);
if (inventoryStuff .numberOfItem < itemClass.maxStack) {
inventoryStuff .iClass = itemClass;
inventoryStuff .numberOfItem += 1;
Debug.Log ((ItemClass)inventoryStuff .iClass);
return;
}
}
}
int firstEmptySlot = emptySlot ();
if (firstEmptySlot >= 0) {
inventoryStuff [firstEmptySlot].iClass = (ItemClass)itemClass;
inventoryStuff [firstEmptySlot].item = itemClass.itemName;
inventoryStuff [firstEmptySlot].numberOfItem = 1;
return;
}
if (firstEmptySlot < 0) {
Debug.Log (“inventory full”);
return;
}
}
int emptySlot() {
for (int i = 0; i <= 4; i++) {
if (inventoryStuff .item == “”) {
return i;
}
}
return -1;
}
public class inventoryItems {
public ItemClass iClass;
public string item;
public int numberOfItem;
public inventoryItems(ItemClass cla, string ite, int num) {
iClass = cla;
item = ite;
numberOfItem = num;
}
}
}
ItemInteraction.cs: handles interactions between player and item tagged objects.
using UnityEngine;
using System.Collections;
public class ItemInteraction : MonoBehaviour {
private Vector3 position;
public GameObject inventoryManager;
void Start() {
position = new Vector3(Screen.width/2, Screen.height/2);
}*_
* void Update () {*
* if(Input.GetKeyDown(KeyCode.E) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) {*
* Collect ();*
* }*
* }*
* void Collect () {*
* Ray interactRay = Camera.main.ScreenPointToRay (position);*
* RaycastHit interactionInfo;*
* if (Physics.Raycast (interactRay, out interactionInfo, 100)) {*
* GameObject collectableObject = interactionInfo.collider.gameObject;*
* if (collectableObject.tag == “item”) {*
* Destroy (collectableObject);*
* inventoryManager.GetComponent ().AddItem (collectableObject.GetComponent ());*
* Debug.Log (collectableObject.GetComponent ());*
* }*
* }*
* }*
* }*
ItemClass.cs: base class for items.
* using UnityEngine;*
* using System.Collections;*
* public class ItemClass : MonoBehaviour {*
* public string itemName = “item”;*
* public int maxStack = 30;*
* public Texture2D itemTexture;*
* public virtual void Use() {*
* }*
* public virtual void Trash() {*
* }*
* }*
I suspect that the storing of the data type ItemClass is where everything is going terribly wrong, though I don’t see why because the class array’s class specifies the ItemClass.