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
-1
Question by BrimTheOne · Jan 30, 2014 at 08:16 PM · listarraysinventoryinventory systemitem

[C#]Inventory script help.

Hello whoever is reading this. I wanted to get more advanced in my programming so i tried to make a Inventory. But there are some stuff i do not understand. and that is why im asking this question. i got my inventoryGUI script, which, makes the GUI. Now how do i add each GUI.Button to a list, or at least create the "effect" of it, so the nearest empty one gets filled with the looted item?

these are my scripts (added ITEMS and ITEM script, so there would be no confusion, if you want to see "WEAPONS.CLASS" Just say so :-) ).

InventoryGUI

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class InventoryGUI : MonoBehaviour {
     public bool render = false;
     public GUISkin kubeSkin;
     
     public List<ITEM> Items = new List<ITEM>();
     public int selectedIndex = -1;
     public Texture2D invSlot = null;
 
     public float posX;
     public float posY;
 
     #region slot textures
     public Texture2D currentMouseIcon = null;
 
     public Texture2D LweaponIcon = null;
     public Texture2D RweaponIcon = null;
 
     public Texture2D hotbar1 = null;
     public Texture2D hotbar2 = null;
     public Texture2D hotbar3 = null;
     public Texture2D hotbar4 = null;
     public Texture2D hotbar5 = null;
     public Texture2D hotbar6 = null;
     public Texture2D hotbar7 = null;
     public Texture2D hotbar8 = null;
     public Texture2D hotbar9 = null;
     public Texture2D hotbar10 = null;
     #endregion
 
     public bool inuse = false;
 
     // Use this for initialization
     void Start () {
 
         if (Items == null)
             
             Items = new List<ITEM>();
     }
     
     // Update is called once per frame
     void ToggleWindow()
     {
         render = !render;
     }
     
     void Update()
     {    
         posX = Input.mousePosition.x;
         posY = Screen.height - Input.mousePosition.y;
         if(Input.GetKeyDown(KeyCode.I))
         {
             ToggleWindow();
         }
     }
 
     void OnGUI()
     {
         GUI.skin = kubeSkin;
         for (int i = 0; i < Items.Count; i++)
             {
             
             if (GUILayout.Toggle(selectedIndex == i, Items[i]._NAME, "Button"))
             selectedIndex = i;
             
         }
         #region WEAPONSLOTS
         //LEFT HAND
         if(GUI.Button(new Rect(100, 550, 50, 50), LweaponIcon)){
             LweaponIcon = switchButton(LweaponIcon);
         }
         //RIGHT HAND
         if(GUI.Button(new Rect(950, 550, 50, 50), RweaponIcon)){
             RweaponIcon = switchButton(RweaponIcon);
         }
         #endregion 
         #region hotbar
 
         if(GUI.Button(new Rect(300, 550, 50, 50), hotbar1)){
             hotbar1 = switchButton(hotbar1);
         }
             
 
         if(GUI.Button(new Rect(350, 550, 50, 50), hotbar2)){
             hotbar2 = switchButton(hotbar2);
         }
 
         if(GUI.Button(new Rect(400, 550, 50, 50), hotbar3)){
             hotbar3 = switchButton(hotbar3);
         }
         if(GUI.Button(new Rect(450, 550, 50, 50), hotbar4)){
             hotbar4 = switchButton(hotbar4);
         }
         if(GUI.Button(new Rect(500, 550, 50, 50), hotbar5)){
             hotbar5 = switchButton(hotbar5);
         }
         if(GUI.Button(new Rect(550, 550, 50, 50), hotbar6)){
             hotbar6 = switchButton(hotbar6);
         }
         if(GUI.Button(new Rect(600, 550, 50, 50), hotbar7)){
             hotbar7 = switchButton(hotbar7);
         }
         if(GUI.Button(new Rect(650, 550, 50, 50), hotbar8)){
             hotbar8 = switchButton(hotbar8);
         }
         if(GUI.Button(new Rect(700, 550, 50, 50), hotbar9)){
             hotbar9 = switchButton(hotbar9);
         }
         if(GUI.Button(new Rect(750, 550, 50, 50), hotbar10)){
             hotbar10 = switchButton(hotbar10);
         }
         #endregion
 
         #region INVENTORY LOOP
         if(render)
         {
             for(int x = 50;x < 360;x += 60){
                 for(int y = 100; y < 950; y += 60){
                     for(int slot = 0; slot < 90; slot++){
                     GUI.Button(new Rect(y, x, 50, 50),invSlot);
                     }
             
                 }
             }
 
         }
 
         #endregion 
 
         if(inuse == true){
             GUI.Box(new Rect(posX - 25, posY - 25, 50, 50), currentMouseIcon);
         }
     }
 
     public Texture2D switchButton(Texture2D curSlot){
 
         if(curSlot == null){
                 if(inuse == true)
                 {
                     curSlot = currentMouseIcon;
                     inuse = false;
 
                 }return curSlot;
             }
 
             else{
                 if(inuse == false){
                     currentMouseIcon = curSlot;
                     curSlot = null;
                     inuse = true;
             }return curSlot;
 
         }
         return curSlot;
 
     }
 
 }



ITEMS -

 using System.Collections.Generic;
 public class ITEMS : MonoBehaviour {
 
     #region WEAPONS
     public List<ITEM> weaponsList;
 
     #region Beginnersword_01
     static GameObject wep01_model;
     static Texture2D wep01_icon;
 
     public WEAPONS wep01;
     #endregion
 
     #region Beginnersword_01
     static GameObject wep02_model;
     static Texture2D wep02_icon;
     
     public WEAPONS wep02;
     #endregion
 
 
     #endregion
     void Awake(){
         if (weaponsList == null)
             weaponsList = new List<ITEM>();
 
         wep01_model = Resources.Load<GameObject>("ITEM MODELS/WEAPONS/Sword/SWORD_MODEL") as GameObject;
         wep01 = new WEAPONS(1,"Sword of the Beginner",wep01_icon,001,1,wep01_model,10f,0);
 
         wep02_model = Resources.Load<GameObject>("ITEM MODELS/WEAPONS/Hammer/HAMMER_MODEL") as GameObject;
         wep02 = new WEAPONS(2,"Hammer of the Beginner",wep02_icon,001,1,wep02_model,10f,0);
 
     }
     void Start () {
 
         weaponsList.Add(wep01);
         weaponsList.Add(wep02);
 
     }
 
 }


ITEM -

 using UnityEngine;
 using System.Collections;
 
 public class ITEM{
     public int _ID;
     public string _NAME;
     public Texture2D _ICON;
     public int _VALUE;
 
     public ITEM(int id, string name,Texture2D icon,int value){
         _ID = id;
         _NAME = name;
         _ICON = icon;
         _VALUE = value;
     }
 
 }
 
 
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 BrimTheOne · Jan 30, 2014 at 10:36 PM 0
Share

Bump......

avatar image Artifactx · Jan 17, 2015 at 05:57 PM 0
Share

Here is a great tutorial, that $$anonymous$$ches you how to create an inventory system from scratch, with the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ and its all done in C#

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ouija · Jan 30, 2014 at 10:59 PM

Hey, I am not an advanced programmer, but check out bergzerg on youtube. I think that is his name. He does a full inventory system. By the time I was done, i was picking up items, viewing them in the inventory, and also for things like weapons and items you pick up, you can put them on your character as well. It's a good rig, try it out! He does it step by step explaining everything. I would show you the code if I knew where I put it.... hmmm

Comment
Add comment · 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
0
Wiki

Answer by james_170482 · Jan 30, 2014 at 11:14 PM

Hello,

I'm Not Sure I completely understand but when i want to dynamically create buttons for an unknown amount of items i do something like this.

     public class Inventory : MonoBehaviour
     {
          public List< string > Items = new List< string >();
     
          //Add Items To List Etc......
     
          void OnGUI()
          {
             if( Items.Count > 0 )
             {
                 for( int i = 0; i < Items.Count; i++ )
                 {
                      if( GUI.Button( new Rect( 50, i * 50, 50, 50 ), Items[ i ].ToString()))
                      {
                           print( "We Clicked Item  " + Items[ i ] );
                      }
                 }
             }
         }
     }

Hope this is some help.

Comment
Add comment · 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
0

Answer by ik1602 · Feb 06, 2015 at 10:02 PM

Since I am making second advanced version, it would really use your opinion on my first asset http://goo.gl/6Bq4Ll this may give you an idea. DIABOLIC RPG INVENTORY-FREE

p.s. I intent to post same response trough the forum whenever similar questions are ask, since I need opinions on my second asset which is advanced version of this one. I hope I am not spamming, if someone thinks that I do please let me know I'll stop immediately.Thanks.

Comment
Add comment · 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

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

22 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

Related Questions

How i can make a script-made button interactable? 0 Answers

Deleting item from inventory system... 1 Answer

[SOLVED] Inventory Stacking Problem [C#] 2 Answers

Preventing Items from shifting in a list 1 Answer

How can I transfer the item's image in inventory UI to the next scene?,How can I keep and transfer the image of items in the inventory in different scenes? 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