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 /
avatar image
0
Question by Reiji92 · Aug 17, 2016 at 01:15 PM · c#databaseinventorypickupinventory system

adding inventory items is half-working

Hello all, I'm really struggling with this one, please help me. First, let me show you the code.`

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour 
 {
     GameObject inventoryPanel;
     GameObject slotPanel;
     itemDataBase database;
     public GameObject inventorySlot;
     public GameObject inventoryItem;
 
     int slotAmount;
     public List<Item> items = new List<Item>();
     public List<GameObject> slots = new List<GameObject>();
 
     void Start()
     {
         database = GetComponent<itemDataBase> ();
 
         slotAmount = 36;
         inventoryPanel = GameObject.Find ("Inventory Panel");
         slotPanel = inventoryPanel.transform.FindChild ("Slot Panel").gameObject;
         for (int i = 0; i < slotAmount; i++) 
         {
             items.Add (new Item ());
             slots.Add (Instantiate (inventorySlot));
             slots [i].transform.SetParent (slotPanel.transform);
         }
 
         AddItem (5);
     }
 
     public void AddItem(int id)
     {
         Item itemToAdd = database.FetchItemByID (id);
         for (int i = 0; i < items.Count; i++) 
         {
             if (items [i].ID == -1) 
             {
                 items [i] = itemToAdd;
                 GameObject itemObj = Instantiate (inventoryItem);
                 itemObj.transform.SetParent (slots [i].transform);
                 itemObj.transform.position = new Vector2(0,0);
                 itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
                 itemObj.name = itemToAdd.Title;
                 break;    
             }
         }
     }
 }
 

alt text This is the Inventory class, as you can see, I called "AddItem" with the ID=5 in Start(). In the first image that I uploaded, you can see that the item indeed shows in my inventory. However, I don't want to call this function here. Let me paste the second code.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class axePickUp : MonoBehaviour {
     
     Inventory inventory;
 
     void Start () {
         inventory = GetComponent<Inventory> ();
     }
 
     void Update(){
         if (Input.GetButtonDown("F")) {
             pickUp();
         }
     }
 
     void pickUp(){
         inventory.AddItem (5);
     }
 }

I want to call this function in axePickUp(). The function kinda knows what I want, but not exactly, as you can see in the second image I uploaded....

alt text

The sprite image just sits there in the corner. PLEASE FOR THE LOVE OF GOD, help me. I have absolutely no idea how to fix this, I tried for days.

inv1-min.png (231.5 kB)
inv2-min.png (238.2 kB)
Comment
Add comment · Show 2
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 Reiji92 · Aug 16, 2016 at 05:02 PM 0
Share

It's a bit hard to see where is the icon exactly in the second image, but here's an other one:

http://kepfeltoltes.hu/160816/inv3_www.kepfeltoltes.hu_.png

avatar image Reiji92 · Aug 17, 2016 at 08:07 AM 0
Share

found the solution! It took me several hours, and my brothers help, but here is the code, we had to change several lines:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : $$anonymous$$onoBehaviour 
 {
     GameObject slotPanel;
     itemDataBase database;
     public GameObject inventorySlot;
     public GameObject inventoryItem;
 
     int slotAmount;
     public List<Item> items = new List<Item>();
     public List<GameObject> slots = new List<GameObject>();
 
     void Start()
     {
         database = GetComponent<itemDataBase> ();
 
         slotAmount = 36;
         slotPanel = GameObject.Find ("Slot Panel");

         for (int i = 0; i < slotAmount; i++) 
         {
             items.Add (new Item ());
             slots.Add (Instantiate (inventorySlot));
         }
     }
     public void AddItem(int id)
     {
         Item itemToAdd = database.FetchItemByID (id);

         for (int j = 0; j < slotAmount; j++) {
             if (items [j].ID == -1) {
                 slots [j].transform.SetParent (slotPanel.transform);
                 items [j] = itemToAdd;
                 GameObject itemObj = Instantiate (inventoryItem);
                 itemObj.GetComponent<ItemData> ().item = itemToAdd;
                 itemObj.transform.SetParent (slots [j].transform);
                 itemObj.GetComponent<Image> ().sprite = itemToAdd.Sprite;
                 itemObj.name = itemToAdd.Title;
                 break;    
             }
         }
     }

}

This solved the problem above, however it's not working perfectly, because it's not loading the slots on Start, but if I add an item to it, for example if I kill a zombie, and it's loot is a sword and a pair of gloves, then it puts it in my inventory, and it shows it properly. The slot amount is 36, I can't carry more items than that, so it's not broken either. summa summarum it's working really well, I don't even care if it doesn't show the slots on start, it's not that necessary, I can live with that.

picture of it: http://kepfeltoltes.hu/160817/inv4_www.kepfeltoltes.hu_.png (for some reason I can't upload it here)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Searching your inventory for an item,How do i search for an item in my inventory? 1 Answer

Object Reference Not Set To An Instance of An Object Error 1 Answer

unity3d simple inventory 1 Answer

Database error updating inventory 0 Answers

Multiple Cars not working 1 Answer


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