- Home /
The question is answered, right answer was accepted
Inventory Script Not Working
Hello so recently after many attempts before I finally tried my hardest to understand tutorials on inventory systems. I found a basic script for inventory in one of the tutorials but the script that I am using is not working. (I did attempt to change a few things to fit in my game) I am able to put the item in my inventory list, but when I tried making a key that uses a certain "Potion" all that happens when I click the F key(The Key to use the item) is that it says is this multiple times (NullReferenceException: Object reference not set to an instance of an object Interactable.Update () (at Assets/Scripts/Interactable.cs:40)
Interactable Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Interactable : MonoBehaviour {
[HideInInspector]
public NavMeshAgent playerAgent;
private bool hasInteracted;
public Inventory inventory;
public string itemType;
public virtual void MoveToInteraction(NavMeshAgent playerAgent)
{
hasInteracted = false;
this.playerAgent = playerAgent;
playerAgent.stoppingDistance = 2f;
playerAgent.destination = this.transform.position;
}
void Update()
{
if(!hasInteracted && playerAgent != null && !playerAgent.pathPending)
{
if(playerAgent.remainingDistance <= playerAgent.stoppingDistance)
{
Interact();
hasInteracted = true;
}
else
{
hasInteracted = false;
}
}
if (Input.GetButtonDown("Use Potion"))
{
//Check the inventory for a potion
GameObject bottle = inventory.FindItemByType("Health Potion");
if (bottle != null)
{
//Use the potion
Debug.Log("Used Potion");
//remove the potion from inventory
inventory.RemoveItem(bottle);
}
}
}
public virtual void Interact()
{
Debug.Log("Interacting with base class");
}
}
Potion Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Potion : Interactable
{
public GameObject currentInteractableObject;
public bool InventoryItem = true;
public override void Interact()
{
gameObject.SetActive(false);
inventory.AddItem(currentInteractableObject);
Debug.Log("Added Potion");
}
}
Inventory Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour {
public GameObject[] inventory = new GameObject[10];
public void AddItem(GameObject item)
{
bool itemAdded = false;
//Find first open slot in inventory
for (int i = 0; i < inventory.Length; i++)
{
if(inventory[i] == null)
{
itemAdded = true;
inventory [i] = item;
Debug.Log(item.name + "Was Added");
break;
}
}
//Inventory Full
if(!itemAdded)
{
Debug.Log("Inventory is Full - Item Not Added");
}
}
public bool FindItem(GameObject item)
{
for (int i = 0; i < inventory.Length; i++)
{
if (inventory[i] != null)
{
if (inventory[i] == item)
{
return true;
}
}
}
//Item Not Found
return false;
}
public GameObject FindItemByType(string itemType)
{
for (int i = 0; i < inventory.Length; i++)
{
if(inventory [i] !=null)
{
if(inventory[i].GetComponent<Interactable>().itemType == itemType)
{
//We Found Item of the type we were looking for
return inventory[i];
}
}
}
//Item of Type not found
return null;
}
public void RemoveItem(GameObject item)
{
for (int i = 0; i < inventory.Length; i++)
{
if(inventory[1] != null)
{
if (inventory[i] == item)
{
//We Found Item remove it
inventory[i] = null;
Debug.Log(item.name + "was removed from the inventory");
break;
}
}
}
}
}
Thank you for taking the time to look at my scripts and helping me!!
Answer by hulahoolgames · Sep 03, 2017 at 11:01 AM
Its giving you Null Reference here:
//Check the inventory for a potion
GameObject bottle = inventory.FindItemByType("Health Potion");
Can you make sure the following object is attached to the Inventory in the inspector? You might have forgotten to drag drop the Inventory.
public Inventory inventory;
I placed the item that holds the inventory($$anonymous$$y Player) in the public Inventory inventory;
I think that the script does not know what bottle is because all the null reference highlight the entire line. GameObject bottle = inventory.FindItemByType("Health Potion");
I would breakpoint on this line and when you hit it, check what inventory is. This line is just assigning a value to "bottle" and its fine even if that is null, you are not accessing any properties of bottle here. The fact that you get null reference means inventory is null. There can be several reasons, $$anonymous$$yPlayer doesn't exist yet or is deleted making this reference null. Best way to find out is break point and see what inventory value is. Let me know.
It says null when I dont have the item and when I have the item. It seems that the bottle or potion will only be deleted or "Used" when its Element 0 in my List other than that it doesnt do anything. $$anonymous$$y Debug.Log notes do pop up but the bottle doesnt get deleted when I use it. All that happens is that it sends the null reference and shows Debug. Other than that the bottle isnt taking out of my List or anything. Sorry For troubling you its just this stuff is very new to me and I am not use to the Loop commands yet. Thank you for taking the time in helping too!