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 /
  • Help Room /
avatar image
0
Question by VeNN0m · Sep 03, 2015 at 12:43 PM · listlistsfor-loop

Inventory slots number are wrong

So I got a code for an inventory system. However something is wrong. Here's the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory1 : MonoBehaviour {
 
 
     public List<GameObject> Slots = new List<GameObject> ();
     public List<Item> Items = new List<Item> ();
     public GameObject slots;
     ItemDatabase database;
     int x = -200;
     int y = 195;
 
     // Use this for initialization
     void Start () {
 
         int Slotamount = 0;
 
         database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase> ();
 
         for (int i = 1; i < 7; i++) {
             for(int k = 1; k < 4; k++){
                 GameObject slot = (GameObject)Instantiate(slots);
                 slots.GetComponent<SlotsScript>().slotNumber = Slotamount;
                 Slots.Add (slot);
                 Items.Add (new Item());
                 slot.transform.parent = this.gameObject.transform;
                 slot.name = "slot" + " " + i + "." + k;
                 slot.GetComponent<RectTransform>().localPosition = new Vector3(x,y,0);
                 x = x + 385;
                 if(k == 2){
                     x = -200;
                     y = y - 78;
                 }
                 Slotamount++;
             }
         }
 
         AddItem (0);
         AddItem (1);
 
     }
 
     void AddItem(int id){
         for (int i = 0; i < database.items.Count; i++) {
             if(database.items[i].itemID == id){
                 Item item = database.items[i];
                 AddItemAtEmptySlot(item);
                 break;
             }
         }
     }
 
     void AddItemAtEmptySlot(Item item){
         for (int i = 0; i < Items.Count; i++) {
             if(Items[i].itemName == null){
                 Items[i] = item;
                 break;
             }
         }
     }
 }
 

That's the inventory code, and here is the slots code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class SlotsScript : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler{
 
     public Item item;
     Image itemImage;
     public int slotNumber;
     Inventory1 inventory1;
     
     // Use this for initialization
     void Start () {
         inventory1 = GameObject.FindGameObjectWithTag ("Inventory1").GetComponent<Inventory1> ();
         itemImage = gameObject.transform.GetChild (0).GetComponent<Image> ();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (inventory1.Items[slotNumber].itemName != null) {
             item = inventory1.Items[slotNumber].itemName;
             itemImage.enabled = true;
             itemImage.sprite = inventory1.Items[slotNumber].itemIcon;
         } else {
             itemImage.enabled = false;
         }
     }
 
     public void OnPointerDown(PointerEventData data){
         Debug.Log (slotNumber);
     }
 
     public void OnPointerEnter(PointerEventData data){
 
     }
 }
 

Now my problem is that my very first slot, named 1.1 gets the slot number 17. Then my second slot is 0 and so on till the last one that is 16. Because of this when I add items in the inventory, they start adding from the second slot. Can someone help me?

Comment
Add comment · Show 5
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 Positive7 · Sep 03, 2015 at 02:29 PM 0
Share

Create a Prefab of the slots and assign it in the inspector ins$$anonymous$$d of leaving it on Scene view. $$anonymous$$ake sure to set its Id to 0 before creating the prefab.

avatar image VeNN0m · Sep 03, 2015 at 04:59 PM 0
Share

@Positive7 I actually did that. I assigned the prefab in the inspector. I see the slots and everything. $$anonymous$$y problem is that the first slot is seen as the last one. Therefore item stacking begins from the second slot.

avatar image Positive7 · Sep 03, 2015 at 05:22 PM 0
Share

Ohh... I remember using this script a long time ago and if I remember right that solved the issue for me. Then I had some more troubles with this Inventory so I decide it to build my own.

avatar image VeNN0m · Sep 04, 2015 at 07:32 PM 0
Share

Hmm. How about that ID setting? How do I set the ID to 0?

avatar image Positive7 · Sep 04, 2015 at 07:53 PM 0
Share

Just set it to 0 in the Inspector before creating the prefab. otherwise it will keep reseting to 17 or whatever number it was(as far as I remember).

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

28 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

Related Questions

Remembering list of camera positions is not working 0 Answers

List.contain only one variable 1 Answer

List.contains doesn't work and add duplicates 1 Answer

How to prevent an overflow? 1 Answer

a list with a constructor class that also contains a list. 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