Item dropping/instantiating isn't working
I have the removal from inventory working, but trying to get the actual item prefab to show up on the ground isn't working. I'm getting a null reference every time that I try to drop an item. Here is my Item code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")] public class Item : ScriptableObject {
new public string name = "New Item"; //Name of the item
public Sprite icon = null; //Item Icon
public bool isDefaultItem = false; //Is this a default item?
public GameObject Object;
public string description;
public virtual void Use ()
{
//Use the item
//Something might happen
Debug.Log("Using" + name);
}
public void RemoveFromInventory()
{
Inventory.instance.Remove(this);
}
}
And here is the code from my inventory script:
public void Remove( Item item)
{
items.Remove(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
print(item.name);
Instantiate(item.Object, player.transform.position + new Vector3(0, -3, 0), player.transform.rotation);
}
}
can you maybe post a picture of the stacktrace of that null reference exception?
Answer by Temseii · May 17, 2018 at 03:39 PM
In the Remove method in your inventory script, what exactly is being called with 'item.Object' in the instantiation? Should it perhaps be 'item.gameObject' instead?
It was a gameObject variable in the item script, called Object for short. But i changed it to this.item to simplify it. But still not working. :/
@ealva479, The reason why it's null, is because that tag that the scripts using doesnt detect whatever you pickup or it cant find that object.
How though? The tag should be working, since I referenced the item at the beginning of the inventory slot script
Answer by browne11 · May 24, 2018 at 05:23 AM
It appears you're deleting the object before you instantiate via items.Remove.
We don't see this part of your code, but that would definitely throw you a null exception.
Try moving that line below your instantiate to see if the problem goes away.
Can you post your items script so I can look at it?
using System.Collections; using System.Collections.Generic; using UnityEngine;
[CreateAsset$$anonymous$$enu(fileName = "New Item", menuName = "Inventory/Item")] public class Item : ScriptableObject {
new public string name = "New Item"; //Name of the item
public Sprite icon = null; //Item Icon
public bool isDefaultItem = false; //Is this a default item?
public GameObject Object;
public string description;
public virtual void Use ()
{
//Use the item
//Something might happen
Debug.Log("Using" + name);
}
public void RemoveFromInventory()
{
Inventory.instance.Remove(this);
}
}
Is there any way that you could help me out with coding my inventory system? You've helped me a lot and I feel like with your help, I could definitely finish it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make a item system with ScriptableObjects where different items can have durability? 1 Answer
Inventory System with Randomly Generated Loot 2 Answers
Removing item from list makes it appear at the bottom ? 1 Answer
Display list as UI or GUI 0 Answers
Multiple tags for one GameObject 3 Answers