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!

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]);
}