Code only runs when object is selected in the editor
I have been working on a "Diablo Style" inventory system for my game. The whole script is run off of the Canvas object. If I run the game with the Canvas selected in the editor, everything runs smoothly. However, if I select any other object, or build the game, the Inventory doesn't build correctly.
I think that it has something to do with a private array that wont let me initialize it. If I make it public and set the values in the inspector window, It works fine. However, if I make it private and initialize it through the code, the inventory doesn't build.
This is a simplified version of the code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
[System.Serializable] public class SlotData { public GameObject slot; public bool isFull;
public SlotData()
{
slot = null;
isFull = false;
}
}
[System.Serializable] public class ItemData { public GameObject item; public GameObject icon; public Vector2Int size; public GameObject slot; public GameObject pickup;
public ItemData()
{
item = null;
icon = null;
size = Vector2Int.zero;
slot = null;
pickup = null;
}
}
public class InventoryManager : MonoBehaviour { public Vector2Int inventorySize; public Sprite inventoryTexture; public Sprite cursorImage;
private GameObject inventoryBackground;
private GameObject inventorySlots;
private GameObject inventoryItems;
private GameObject cursor;
[HideInInspector]
public GameObject heldItem;
private Vector2Int heldItemDimensions;
private int heldItemNumber;
private GameObject heldItemIcon;
public ItemData[] items;
public SlotData[] slots;
private int numberOfItems;
private bool toggleBool;
private bool InventoryIsOpen;
private GameObject maxSlot;
private GameObject minSlot;
void Start ()
{
items = new ItemData[inventorySize.x * inventorySize.y];
slots = new SlotData[inventorySize.x * inventorySize.y];
CreateInventory(inventorySize, inventoryTexture, inventoryBackground);
CreateSlots(inventorySize, inventoryTexture);
}
void CreateInventory (Vector2 size, Sprite texture, GameObject inventory) { inventory = new GameObject("Inventory"); RectTransform back = inventory.AddComponent(); inventory.transform.SetParent(this.transform); back.anchorMin = new Vector2((1 - (size.x / 10)) / 2, (1 - (size.y / 10)) / 2); back.anchorMax = new Vector2(1 - ((1 - (size.x / 10)) / 2), 1 - ((1 - (size.y / 10)) / 2)); inventory.AddComponent().sprite = texture; inventory.GetComponent().type = Image.Type.Sliced; back.anchoredPosition = Vector2.zero; back.sizeDelta = Vector2.zero; inventoryBackground = inventory;
// Slot Holder
inventorySlots = new GameObject("Inventory Slots");
inventorySlots.transform.SetParent(inventory.transform);
inventorySlots.AddComponent<RectTransform>().anchorMin = Vector2.zero;
inventorySlots.GetComponent<RectTransform>().anchorMax = Vector2.one;
inventorySlots.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
inventorySlots.GetComponent<RectTransform>().sizeDelta = Vector2.zero;
// Icon Holder
inventoryItems = new GameObject("Inventory Icons");
inventoryItems.transform.SetParent(inventory.transform);
inventoryItems.AddComponent<RectTransform>().anchorMin = Vector2.zero;
inventoryItems.GetComponent<RectTransform>().anchorMax = Vector2.one;
inventoryItems.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
inventoryItems.GetComponent<RectTransform>().sizeDelta = Vector2.zero;
// Cursor
cursor = new GameObject("Cursor");
cursor.transform.SetParent(inventory.transform);
cursor.gameObject.AddComponent<Image>().sprite = cursorImage;
cursor.GetComponent<RectTransform>().localScale = new Vector2(0.25f, 0.25f);
cursor.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
}
void CreateSlots (Vector2 size, Sprite texture)
{
int i = 0;
for(int x = 0; x < size.x; x++)
{
for(int y = 0; y < size.y; y++)
{
i++;
GameObject slot = new GameObject("Slot " + (x + 1) + ", " + (y + 1));
slot.transform.SetParent(inventorySlots.transform);
slot.AddComponent<Image>().sprite = texture;
slot.GetComponent<Image>().type = Image.Type.Sliced;
slot.GetComponent<RectTransform>().anchorMin = new Vector2(0.01f + ((0.98f / size.y) * y), 0.01f + ((0.98f / size.x) * x));
slot.GetComponent<RectTransform>().anchorMax = new Vector2(0.01f + ((0.98f / size.y) * (y + 1)), 0.01f + ((0.98f / size.x) * (x + 1)));
slot.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
slot.GetComponent<RectTransform>().sizeDelta = Vector2.zero;
slots[i - 1].slot = slot.gameObject;
}
}
}
}
It always seems to stop working at CreateSlots with a NullReferenceException. Did I initialize or construct the arrays wrong?
The code really got messed up when I pasted it in. Sorry about that, I'm not sure how to fix it.