Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 26, 2017 at 02:01 AM by Agent27765 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Agent27765 · Sep 02, 2017 at 11:15 PM · script.inventory system

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!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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;


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Agent27765 · Sep 03, 2017 at 12:44 PM 0
Share

I placed the item that holds the inventory($$anonymous$$y Player) in the public Inventory inventory;

avatar image Agent27765 · Sep 03, 2017 at 12:52 PM 0
Share

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");

avatar image hulahoolgames Agent27765 · Sep 03, 2017 at 05:27 PM 0
Share

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.

avatar image Agent27765 hulahoolgames · Sep 03, 2017 at 06:53 PM 0
Share

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!

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question about an inventory system idea 2 Answers

Problem with inventory asset' script and its working 0 Answers

How to get the current items in my inventory with respective stock of the items 1 Answer

How to make a advanced inventory system? 0 Answers

How to make a savable inventory? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges