• 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 NutellaDaddy · Mar 28, 2014 at 01:38 AM · c#guilistinventory system

Having trouble moving item from one list to another!

I am trying to mkae it so that you can get something out of a chest and into your inventory by clicking on it(later I will make it so that you have to drag it) ,but I can't seem to get it too work because it says the variable that I am using in the for loop hasn't been defined yet for one of the loops. I am going to let you see both of the scripts and then please tell me how I could fix this or do it a different way that wouldn't involve much modification of my scripts! Thank you Heres the crate script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class OpenCrateScript : MonoBehaviour 
 {    
     static public bool lootCrateOpen = false;    //If the loot crate is open or not    
     private float maxDistance;                    //Max distance the player can be from the chest to open it.
     public Transform player;                    //The player opening the chest
     public GameObject crate;                    //The crate that we want to delete or spawn at certain times.
 
     public Texture2D CrateWindow;
     public Texture2D CloseButton;
     public GUIStyle CrateSlotStyle;
     public GUIStyle CrateButtons;
     public GUIStyle CrateScrollBar;
     public GUIStyle Text;
     public GUIStyle Empty1;
     
     public static List<Item> lootItems;
     private int buttonWidth = 66;
     private int buttonHeight = 66;
     private int offsetX = 217;
     private int offsetY = 62;
     private int maxCols = 3;
 
     //LOOT WINDOW SCROLL VIEW
     private Vector2 crateWindowScrollView= Vector2.zero;
     void Start () 
     {
         maxDistance = 2.5f;
         lootItems = new List<Item> ();
         PopulateCrate ();
     }
     void Update ()
     {
         if (craftingMenu.craftingMenuOn == true || BuildingMenu.buildingMenuOn == true) 
         {
             lootCrateOpen = false;
         }
         if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == false) {
             RaycastHit hit;
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
                 if (hit.collider.CompareTag("Loot Crate") && Vector3.Distance(hit.collider.transform.position, player.transform.position) <= maxDistance) {
                     lootCrateOpen = true;
                     inventorySystem.inventoryOn = true;
                     inventorySystem.toolbarOn = true;
                 }
             }
             if ( lootCrateOpen == true && Input.GetKeyUp(KeyCode.Tab))
             {
                 inventorySystem.inventoryOn = false;
                 inventorySystem.toolbarOn = false;
                 lootCrateOpen = false;
             }
         }
         if (inventorySystem.inventoryOn == true && Input.GetKeyUp (KeyCode.Tab)) 
         {
             lootCrateOpen = false;
         }
 
     }
     void OnGUI()
     {
         if (lootCrateOpen == true) 
         {
             GUI.BeginGroup(new Rect(701,31,690,800), CrateWindow);
                 inventorySystem.inventoryOn = true;
                 inventorySystem.toolbarOn = true;
                 GUI.Label(new Rect(220,25,100,20),"Small Crate",Text);
                     if(GUI.Button(new Rect(430,25,15,15),CloseButton,Text))
                     {
                         lootCrateOpen = false;
                         inventorySystem.inventoryOn = false;
                         inventorySystem.toolbarOn = false;
                     }
                 crateWindowScrollView = GUI.BeginScrollView(new Rect(-5,55,450,285), crateWindowScrollView,new Rect(-15,0,42,700));
                     for(int cnt = 0; cnt < 18; cnt++){
                         if(cnt < lootItems.Count)
                             {
                                 GUI.Button (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),cnt.ToString (),CrateSlotStyle);
                             }
                         else
                             {
                                 GUI.Label (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),string.Empty,CrateSlotStyle);
                             }
                     }
                     GUI.EndScrollView();
 
             GUI.EndGroup();
         }
     }
     void OnMouseEnter()
     {
 
     }
     private void PopulateCrate()
     {
         for (int cnt = 0; cnt < 5; cnt++) 
         {
             lootItems.Add (new Item());
         }
     }
 }

Here's the inventory script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class inventorySystem : MonoBehaviour 
 {
     //PUBLIC VARIABLES
     public static bool inventoryOn = false;
     public static bool toolbarOn = false;
     public Texture inventoryWindow;
 
     //GUI FONT,SIZE, STYLE, AND LAYOUT
     public Vector2 windowPosition = new Vector2(0,0);
     public Vector2 windowSize = new Vector2(800,800);
 
     public GUIStyle inventorySlotStyle;
     public GUIStyle inventoryLabelStyle;
     public GUIStyle inventoryTabStyle;
     public GUIStyle backgroundStyle;
     public GUIStyle Empty;
 
     //MAX WEIGHT
     public int weiLimit = 150;
     public int curWei = 0;
     
     //TOOLBAR
     public static int toolbarInt = -1;
 
     //INVENTORY
     private int columns = 6;
     public static List<Item> inventory = new List<Item>();
 
     void Start () 
     {
         toolbarInt = -1;
     }
     void Update () 
     {
         //INVENTORY ON AND OFF
         if (Input.GetKeyUp (KeyCode.Tab)) {
                         if (inventoryOn == false) {
                                 inventoryOn = true;
                                 toolbarInt = 0;
                         } else if (craftingMenu.craftingMenuOn == true) {
                                 toolbarInt = -1;
                         } else if (BuildingMenu.buildingMenuOn == true) {
                                 toolbarInt = -1;
                         } else if (inventoryOn == true) {
                                 toolbarInt = -1;
                         }
                 } 
             //TOOLBAR ON AND OFF
             if (Input.GetKeyUp (KeyCode.Tab) && toolbarOn == false) 
             {
                 toolbarOn = true;
             } 
             else if (Input.GetKeyUp (KeyCode.Tab) && toolbarOn == true) 
             {
                 toolbarOn = false;
                 toolbarInt = -1;
                 craftingMenu.craftingMenuOn = false;
             }
                 //MENUS ON AND OFF
                 if (toolbarInt == -1) {
                         inventoryOn = false;
                         craftingMenu.craftingMenuOn = false;
                         BuildingMenu.buildingMenuOn = false;
                 } else if (toolbarInt == 0) {
                         inventoryOn = true;
                 } else if (toolbarInt == 1) {
                         craftingMenu.craftingMenuOn = true;
                         BuildingMenu.buildingMenuOn = false;
                         inventoryOn = false;
                 } else if (toolbarInt == 2) {
                         BuildingMenu.buildingMenuOn = true;
                         craftingMenu.craftingMenuOn = false;
                         inventoryOn = false;
                 }
         if ( OpenCrateScript.lootCrateOpen == true && Input.GetKeyUp(KeyCode.Tab))
         {
             inventoryOn = false;
             toolbarOn = false;
             toolbarInt = -1;
             OpenCrateScript.lootCrateOpen = false;
         }
     }
     
     void OnGUI()
     {
         if (toolbarOn == true)
         {
             if(GUI.Button (new Rect(167, 10, 140, 40),"Inventory", inventoryTabStyle))
             {
                 toolbarInt = 0;
             }
             if(GUI.Button (new Rect(312, 10, 140, 40),"Crafting", inventoryTabStyle))
             {
                 toolbarInt = 1;
             }
             if(GUI.Button (new Rect(457, 10, 140, 40),"Building", inventoryTabStyle))
             {
                 toolbarInt = 2;
             }
         }
         //INVENTORY
             //INVENTORY QUICK SELECT BAR BACKGROUND
         GUI.BeginGroup (new Rect (380, 345, 604, 415),string.Empty, backgroundStyle);
             //INVENTORY QUICK SELECT BAR
             //SLOT 1
             GUI.Button (new Rect(70,168,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect (70,168,66,66),"1", inventoryLabelStyle);
             //SLOT 2
             GUI.Button (new Rect(148,168,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(149,168,66,66),"2", inventoryLabelStyle);
             //SLOT 3
             GUI.Button (new Rect(226,168,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(227,168,66,66),"3", inventoryLabelStyle);
             //SLOT 4
             GUI.Button (new Rect(304,168,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(305,168,66,66),"4", inventoryLabelStyle);
             //SLOT 5
             GUI.Button (new Rect(382,168,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(383,168,66,66),"5", inventoryLabelStyle);
             //SLOT 6
             GUI.Button (new Rect(460,168,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(461,168,66,66),"6", inventoryLabelStyle);
         GUI.EndGroup ();
 
         //INENTORY ON
         if (inventoryOn == true) 
         {
             //INVENTORY WINDOW
             GUI.BeginGroup (new Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y),inventoryWindow);
             int cnt = 0;
             for(int slot = 0; slot < 24; slot++)
                 {
                 if(cnt < inventory.Count)
                     {
                         GUI.Button (new Rect((slot % columns) * 66 + 280, slot / columns * 66 + 75,66,66),string.Empty,inventorySlotStyle);
                     }
                 else
                     {
                         GUI.Label (new Rect((slot % columns) * 66 + 280, slot / columns * 66 + 75,66,66),string.Empty,Empty);
                     }
                     cnt++;
                 }
             //EQUIP SLOTS
             //HEAD
             GUI.Button (new Rect(42,66,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(47,67,70,70),"Head", inventoryLabelStyle);
             //TORSO
             GUI.Button (new Rect(42,137,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(47,138,70,70),"Chest", inventoryLabelStyle);
             //LEGS
             GUI.Button (new Rect(42,210,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(47,211,70,70),"Legs", inventoryLabelStyle);
             //FEET
             GUI.Button (new Rect(42,283,66,66),string.Empty, inventorySlotStyle);
             GUI.Label (new Rect(47,284,70,70),"Feet", inventoryLabelStyle);
 
             //WEIGHT
             GUI.Label(new Rect(631,360,95, 40), "Weight: " + curWei + " / " + weiLimit, inventoryLabelStyle);
 
             GUI.EndGroup ();
         }
     }
 }

If you need any more information just ask! And thanks again for any help!

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 ArkaneX · Mar 28, 2014 at 08:50 AM 0
Share

Could you post the exact error message?

avatar image senad · Mar 28, 2014 at 09:29 AM 1
Share

Be sure it includes which lines throws the error! :)

avatar image NutellaDaddy · Mar 28, 2014 at 11:59 AM 0
Share

private void AddItem() { inventorySystem.inventory.Add (lootItems [cnt]); }

The name cnt deos not exist in the current context. (Even though I declared it in the for loop.

avatar image NutellaDaddy · Mar 28, 2014 at 12:02 PM 0
Share
 public class OpenCrateScript : $$anonymous$$onoBehaviour 
 {    
     static public bool lootCrateOpen = false;    
     private float maxDistance;        
     public Transform player;        
     public GameObject crate;        
     public Texture2D CrateWindow;
     public Texture2D CloseButton;
     public GUIStyle CrateSlotStyle;
     public GUIStyle CrateButtons;

public GUIStyle CrateScrollBar; public GUIStyle Text; public GUIStyle Empty1; public static List lootItems; private int buttonWidth = 66; private int buttonHeight = 66; private int offsetX = 217; private int offsetY = 62; private int maxCols = 3; private Vector2 crateWindowScrollView= Vector2.zero; void Start () { maxDistance = 2.5f; lootItems = new List (); PopulateCrate (); } void Update () { if (crafting$$anonymous$$enu.crafting$$anonymous$$enuOn == true || Building$$anonymous$$enu.building$$anonymous$$enuOn == true) { lootCrateOpen = false; } if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E) && lootCrateOpen == false) { RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) { if (hit.collider.CompareTag("Loot Crate") && Vector3.Distance(hit.collider.transform.position, player.transform.position)

     }
     void OnGUI()
     {
         if (lootCrateOpen == true) 
         {
             GUI.BeginGroup(new Rect(701,31,690,800), CrateWindow);
                 inventorySystem.inventoryOn = true;
                 inventorySystem.toolbarOn = true;
                 GUI.Label(new Rect(220,25,100,20),"Small Crate",Text);
                     if(GUI.Button(new Rect(430,25,15,15),CloseButton,Text))
                     {
                         lootCrateOpen = false;
                         inventorySystem.inventoryOn = false;
                         inventorySystem.toolbarOn = false;
                     }
                 crateWindowScrollView = GUI.BeginScrollView(new Rect(-5,55,450,285), crateWindowScrollView,new Rect(-15,0,42,700));
                     for(int cnt = 0; cnt < 18; cnt++){
         if(cnt < lootItems.Count)
                             {
                                 GUI.Button (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),cnt.ToString (),CrateSlotStyle);
                             }
                         else
                             {
                                 GUI.Label (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),string.Empty,CrateSlotStyle);
                             }
                     }
                     GUI.EndScrollView();
 
             GUI.EndGroup();
         }
     }
     private void PopulateCrate()
     {
         for (int cnt = 0; cnt < 5; cnt++) 
         {
             lootItems.Add (new Item());
         }
     }
     private void AddItem()
     {
         inventorySystem.inventory.Add (lootItems [cnt]);
     }
 }
 It's in the function right above me^^^^^^

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ArkaneX · Mar 28, 2014 at 12:17 PM

A variable used in a for loop is only valid in a scope of this loop. It is not visible outside.

There are additional errors in your script as well:

  • AddItem method is never called

  • Your gui buttons are not tested for click

You need to test for click, and if a button is clicked, call AddItem passing cnt value as parameter. Pseudocode:

 if(GUI.Button(...))
 {
     AddItem(cnt);
 }
 private void AddItem(int cnt)
 {
    inventorySystem.inventory.Add (lootItems [cnt]);
 }
Comment
Add comment · Show 2 · 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 NutellaDaddy · Mar 28, 2014 at 09:35 PM 0
Share

Thank you ,but can you tell me how I could also make it remove the item in the other list? Along with what it already does in the function?

avatar image ArkaneX · Mar 28, 2014 at 11:03 PM 0
Share
 private void AddItem(int cnt)
 {
    inventorySystem.inventory.Add(lootItems [cnt]);
    lootItems.RemoveAt(cnt);
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

How to make spaces in between Gui boxes being made in for loops? 1 Answer

Which is better for an inventory system, Array or List? 0 Answers

Setting Scroll View Width GUILayout 1 Answer

A node in a childnode? 1 Answer

Adding Item object to Inventory List 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