Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Iceblitzyt · Sep 03, 2013 at 11:58 AM · inventoryc# dll

Need expert help fixing my inventory. c#

hey there,

So I'm trying to create an inventory system, I've gotten to the point now where icons drag and drop into the inventory, but due to the fact i wish to make it both X and Y, i have the issue where once an item goes in, all slots become that item..

Can someone look at this code and help me fix it please:

[CODE]

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerHUD : MonoBehaviour {
     
     //ActionBar
     public SpellDataBase[] ActionBar = new SpellDataBase[12];
     public List<SpellDataBase> _SpellBook = new List<SpellDataBase>();
     public ItemDataBase[] InventoryBars = new ItemDataBase[1];
     public List<ItemDataBase> _ItemList = new List<ItemDataBase>();
     private SpellDataBase DraggedSpell;
     private ItemDataBase DraggedItems;
     private ItemDataBase DraggedItems2;
     
     private int xx;
     private int yy;
 
 
     void Start () {
     CurrentScore = PlayerPrefs.GetInt("CurrentScore");
     CharactersName = PlayerPrefs.GetString("CharactersName");    
         #region Spells 
         _SpellBook.Add(new SpellDataBase.FireBall(this));
         _SpellBook.Add(new SpellDataBase.IceBall(this));
         #endregion 
     // Divide...
         #region Items
         _ItemList.Add(new ItemDataBase.Wood(this));
         _ItemList.Add(new ItemDataBase.Wheat(this));
         #endregion
         
     }
     
     void OnGUI () {
 
     //if(Triggerinventory){            
         #region Inventory
         int Inventoryx =0;    
         int _Offset = 0;
         foreach (ItemDataBase Inventory_Item in InventoryBars)
         {
 
             Texture2D icon = Resources.Load("emptyactionbutton") as Texture2D;
 
             if(Inventory_Item != null)
                 icon = Inventory_Item.icon;
 
               for(int xx = 0; xx < 5; xx++){
             for(int yy = 0; yy < 5; yy ++){        
             
             Rect Inventory_icon_rect_Items = new Rect(Screen.width/2 + ( xx * 60), 50 + (yy * 60), 60, 60);
 
             GUI.DrawTexture(Inventory_icon_rect_Items,icon);
             _Offset += 54;
 
 
             if (DraggedItems != null && Inventory_icon_rect_Items.Contains(Event.current.mousePosition) && !Input.GetButton("Fire1"))
             {
                 InventoryBars[Inventoryx] = DraggedItems;    
                 DraggedItems = null;
             }
 
             else if (DraggedItems == null && Inventory_icon_rect_Items.Contains(Event.current.mousePosition) && Input.GetButtonDown("Fire1"))
             {
                 DraggedItems = Inventory_Item;
                 InventoryBars[Inventoryx] = null;
             }
             }
             }
         
            // Inventoryx++;    
         }
     //}    
         #endregion
     
             
     
     
         // _Dragged Items
         #region Dragged Items
          if (DraggedItems != null)
         {
             Rect rec = new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 0, 0);
             //Rect rec = mouse_pointer;
             rec.width = DraggedItems.icon.width;
             rec.x -= DraggedItems.icon.width / 2;
             rec.y -= DraggedItems.icon.height / 2;
             rec.height = DraggedItems.icon.height;
             GUI.DrawTexture(rec, DraggedItems.icon);
         }
 
         //Whenever we release the drag,"delete" the spell we are moving
         if (!Input.GetButton("Fire1"))
             DraggedItems = null;
         
         #endregion
         }
 }

[/CODE]

As you can see it basically allows the user to drag items from the item script, which is this:

[CODE]

 using UnityEngine;
 using System.Collections;
 
 public abstract class ItemDataBase  {
     private string ItemName;
     private int Value;
     private string ItemType; // This will be the type of item, this will range from, ( uncommon, common, good, rare, epic, heroic, legendary ) 
     
     
     public Texture2D icon;
     private PlayerHUD owner;
 
     
     
     
     public abstract void ItemClicked();
     
     #region Misc Items
     
     public class Wood : ItemDataBase
     {
         public Wood(PlayerHUD owner)
         {
             this.ItemName = "Wood";
             this.Value = 10;
             this.ItemType = "Common";
             this.owner = owner;
             this.icon = Resources.Load("WoodIcon") as Texture2D;
         }
 
         public override void ItemClicked()
         {
             Debug.Log(ItemName +"Value: " + Value + "Item Type: " + ItemType + "Description: " + "Smells kinda woody...");
         }
     }
 
     public class Wheat: ItemDataBase
     {
         public Wheat(PlayerHUD owner)
         {
             this.ItemName = "Wheat";
             this.ItemType = "Common";
             this.Value = 2;
             this.owner = owner;
             this.icon = Resources.Load("wheat") as Texture2D;
         }
 
         public override void ItemClicked()
         {
             Debug.Log("Some mighty fine wheat...");
         }
     }
     #endregion
     
     #region Weapons
     
     #endregion
     
     #region Armour
     
     #endregion
     
     #region Consumables
     
     #endregion
     
 }

[/CODE]

Anyways, it only works if i make inventory bars 1, which i can understand. This will make each box that item though, when i take out the for loops for xx and yy i just get a set of bars going in a straight line.. can someone tell me how i can alter this to make a loot window which can be usable to the draggeditems part etc.

Many thanks, Ice

Comment
Add comment · Show 4
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 -hiTo- · Sep 03, 2013 at 06:08 PM 0
Share

What's the difference between InventoryBars and _ItemList? And why are you looping through the inventory slots while looping through the InventoryBars?

I don't follow your reasoning, is all - and a few comments or a pseudo-code interpretation from you would go a long way to figure out just what it is you want to do.

Even a screenshot of it in action would be great.

avatar image -hiTo- · Sep 03, 2013 at 11:32 PM 1
Share

This is how I would do it:

 private List<Item> Inventory;

 private void Start()
 {
     Inventory = new List<Item>();
 }
 
 private void DrawInventoryItems()
 {
     Vector2 currentInventoryPosition = Vector2.zero;
     foreach (item in Inventory)
     {
         DrawItem(item, currentInventoryPosition);
         
 
         currentInventoryPosition.x++;
         if (currentInventoryPosition.x > maxGridPlaces.x)
         {
             //If it should drop down a row...
             currentInventoryPosition.y++
             currentInventoryPosition.x = 0;
         }
     }
 }

 private void DrawItem(Item itemToDraw, Vector2 inventoryOffset)
 {
     // Draw item at inventoryPosition.x + gridsize.x * inventoryOffset.x
     // And inventoryPosition.y + gridsize.y * inventoryOffset.y
 }

Now it's a simple task to, when you drop an item in the inventoryRect, check if a slot is empty, then add it to the list. You can rearrange the inventory by rearranging the list, if you want. (Lists have a handy Sort()-function, as well.)

avatar image Iceblitzyt · Sep 04, 2013 at 04:22 PM 0
Share

I kind of understand what you mean, can you possibly redo it in c#, I don't have a solid background in Java

avatar image -hiTo- · Sep 04, 2013 at 04:31 PM 1
Share

$$anonymous$$ost of that is c#. You just need to set the Inventory to private (or public...) and set a return type to the function (i.e. private void DrawInventoryItems()).

Then call the function from OnGUI(). You also need to create the DrawItem-function yourself, but it should be fairly easy (you have already made it in your original code, pretty much.)

I updated the code a bit.

Let me know if you need any more help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Sep 03, 2013 at 06:29 PM

From what I gathered from studying your code, it seems you have only one slot in your inventory, which you draw multiple times: public ItemDataBase[] InventoryBars = new ItemDataBase[1];. This seems to be the cause of the problem.

As a fix, I would suggest you create more inventory slots, then iterate through them like you do currently, only put inventoryx within the for loops. I would try inside the second for loop for starters.

Comment
Add comment · Show 1 · 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 Iceblitzyt · Sep 03, 2013 at 10:57 PM 0
Share

I've just tried to do that and I can't get it to work. Can you please post and example so i can have a look? $$anonymous$$any thanks.

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

18 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

Related Questions

unity3d simple inventory 1 Answer

how to select weapon in main menu 1 Answer

Display Item Sprite on Inventory UI 1 Answer

Can I use Scriptable Objects for Hero Stats that change throughout the game? 0 Answers

How can I delete an item from this inventory system? 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