In these days of crisis for Covid19 and inevitably I have to stay at home I decided to create a reference script for my test level; Inventory … arghhhhh!!!
Ok, As already seen and reviewed (also thanks to my old treads) I’ve create a ScriptableObject for information and image for my all kry Object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu (menuName = "ScripObj", fileName ="InfoObj")]
public class ObjectInformation : ScriptableObject
{
public string itemName;
public string description;
public Sprite im;
public int ID;
}
Then I created a Canvas with its 8 slots (button) and I attached the Inventory script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public Image[] itemImages = new Image[numItemSlots];
public ObjectInformation[] arrSlots = new ObjectInformation[numItemSlots];
public const int numItemSlots = 8;
public void AddItem(ObjectInformation itemToAdd)
{
for (int i = 0; i < arrSlots.Length; i++)
{
if (arrSlots[i] == null)
{
arrSlots[i] = itemToAdd;
itemImages[i].sprite = itemToAdd.im;
itemImages[i].enabled = true;
return;
}
}
}
public void RemoveItem(ObjectInformation itemToRemove)
{
for (int i = 0; i < arrSlots.Length; i++)
{
if (arrSlots[i] == itemToRemove)
{
arrSlots[i] = null;
itemImages[i].sprite = null;
itemImages[i].enabled = false;
return;
}
}
}
}
And I in another script call the function AddItem with:
As you can see, in itemImages[i].sprite = itemToAdd.im; I have associated the image present in the ScriptableObject inside the image of the button. When I play the game and take the object, I have the error: “NullReferenceException: Object reference not set to an instance of an object Inventory.AddItem (ObjectInformation itemToAdd) (at Assets/Script/Inventory/Inventory.cs:21)”
As we know, this error occurs when there is no gameobject associated, but in this case everything is in place. In fact, if I try to delete in the Inventory script itemImages[i].sprite = itemToAdd.im;, all work well and I have the basic texture of the button in the first slot ecc.
Now it is probably a nonsense of a problem, but I have not found a solution for hours… any tips? Frankly to me the part of the inventory has always driven me crazy…
Pretty sure that would mean your inventory variable is null. Is that set up? You don’t show its declaration or its data provenance, so not really sure how it gets set.
I have another script attached in an EmptyObject with the child my object to take.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Item : MonoBehaviour
{
public ObjectInformation referenceScriptableObject;
[Space]
public Inventory inventory;
[Space]
public Text _name;
private Text _str;
public Image _im;
public int _id;
[Space]
public GameObject panelCanvasObj;
private void Start()
{
gameObject.tag = "Respawn";
_name.text = referenceScriptableObject.itemName;
//_str.text = referenceScriptableObject.description;
_im.sprite = referenceScriptableObject.im;
_id = referenceScriptableObject.ID;
}
private void Update()
{
if (inventory == null)
{
inventory = GameObject.FindObjectOfType<Inventory>();
}
}
public void DestroyItm()
{
ObjectInformation itemToAdd = null;
inventory.AddItem(itemToAdd);
Destroy(gameObject);
}
public void ClosedPanel()
{
FindObjectOfType<PlayerOnOff>().ShowPlayer();
panelCanvasObj.SetActive(false);
}
}
Since I have multiple Canvases to display information and images, I take this item as a reference. Everything works well, only the problem with inventory is wrong…
I called the function AddItem() from the script “Item” directly within the “Inventory” script and not from the “Item” script itself. So instead of writing:
public void DestroyItm()
{
inventory.AddItem(referenceScriptableObject);
//ecc.
}
where “referenceScriptableObject” is the indentifier from variable “ObjectInformation” script, I was writing:
public void DestroyItm()
{
inventory.AddItem(itemToAdd);
//ecc.
}
taking directly the value itemToAdd in Inventory script (and not the argument in Item script)
Thanks to @Kurt-Dekker for routing me on the right track
I will reiterate. I suggest you learn how to use the debugger. It’s not very complicated, and doesn’t take very long to learn, and it saves you so much time and frustration that it is one of the first things that a person should learn when they are learning how to code.
An error like this would probably have taken you 5 to 10 minutes to solve with the debugger.
Just learn:
-How to attach the debugger to unity
-Setting breakpoints
-Mousing over variables to check their values
-Stepping through code and controlling execution flow (step over, step into, run to statement, set next statement)
-Looking at the call stack which tells you information about execution flow on a larger scale (for example, what function called the function we are currently in… and what function called THAT function etc…)