Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# GUI Inventory Crafting Grid. Am i doing it right?

Discussion in 'Scripting' started by GingerNinja04, Jul 10, 2014.

  1. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    Hi guys,

    I'm not exactly a Great Coder, but I have been watching a lot of tutorials on Unity and have started to adapt one of the Inventory System tutorials into my own adding a small 3x3 crafting Grid along with a "Hot Bar".

    My question is not what is the code, but more have i done this right? I just realised the way i have set up my basic crafting would mean if i had say materials to make 2 different items My crafting would give me both items instead of just the one. (Best way to explain Mine craft. I want to build a Pick axe and when i click craft I will recieve a shovel, PickAxe and a Hoe.)

    My script is very basic:
    Code (CSharp):
    1.  
    2. /*
    3. Grid =     0 1 2
    4.                3 4 5
    5.                6 7 8
    6. Cinv[6]=Crafting Inventory[Slot]
    7. */
    8. //Stone PickAxe
    9.         if ((Cinv [6].itemName == "Stick") && (Cinv [4].itemName == "Stick") && (Cinv [5].itemName == "Stone")&& (Cinv [1].itemName == "Stone")) {
    10. /* Use ItemID when Removing or Adding Items */
    11.             RemoveItemCrafting (1);
    12.             RemoveItemCrafting (1);
    13.             RemoveItemCrafting (2);
    14.             RemoveItemCrafting (2);
    15.             AddCraftedItem (0);
    16.            
    17.         }
    18.  
    Pretty much explained but Incase you don't understand it. I am using these If Statements inside a Void Crafting.
    When i press the crafting button it runs through the Script. so if crafting slot 4 & 6 contain a stick and slot 1 & 5 has stone. it will remove them from the crafting Grid and add a StoneAxe to my Players Inventory.

    I have already Explain how i don't think this is very effective. but I can't think of another way except for maybe adding in all 9 slots and do a check for Null on the slots not being used.

    Does anyone have any Suggestions?
    I don't want you to hand feed me a Script. But a simple push in the right Direction with a little explanation enough so I can Attempt to Learn by doing :)

    The game i have in mind for this script is a Man Vs Wild survival Staged on a large Island. so basically "The Forest" with less Mutants and more Grizzly's :p

    Cheers,
    Jamie
     
  2. MorganFruehman

    MorganFruehman

    Joined:
    May 13, 2014
    Posts:
    10
    One easy way of fixing this would be to check the number of items in your crafting grid in the beginning of the function. After this you compare the number of items in the grid with the number of needed items for the craftables. Only if those numbers are equal you do the check you posted.
     
    GingerNinja04 likes this.
  3. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    I don't understand your approach. What happens if slot 3 and 5 contains a stick nad slot 2 and 6 has a stone? Do the mentioned items be in those specific slots?
     
  4. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    I think what Morgan was thinking about is instead of placing your items on a specific slot, you allow the player to put the items in the grid (where he wants) and you only check how many similar items are on the grid.
    What I was about to say is that you should have somewhere in your crafting class or somewhere else a value that can tell you how many items you need for each crafted item. Doing a check like that :
    will become really complicated to maintain, especially that if you decide to change the number of needed items, you'll have to go through all your scripts again.

    I didn't really answer your question, but I wanted to jumpin to give you some tips you could apply as you go :) if you want.

    Hope it can help you.
     
  5. MorganFruehman

    MorganFruehman

    Joined:
    May 13, 2014
    Posts:
    10
    Perhaps i didn't fully understand the problem, but let me try to explain what i understood.

    Say we have two craftable items. One is a spear and you need 3 sticks in the slots 1, 4 and 7. The second one is a pickaxe, again 3 sticks in 1, 4, 7 and a stone in slot 2. Now you have everything set up for a pickaxe and start crafting. The result is that you get a pickaxe AND a spear, because you also have everything in place for a spear. And that is your problem, right?
    If so you can count all items in the grid (would be 4 when trying to craft a pickaxe). Then when you check which item should be crafted (with your if statements) you compare the number of items in the grid with the number of items needed for the currently checked craftable. So when your function checks to see if it should build a spear it notices that you have 4 items in the grid but the spear only needs 3 items. At this place it should skip the spear-check and go on to the next.
    If that isn't your problem, sorry :p perhaps you could try to explain it again than?
     
    GingerNinja04 likes this.
  6. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    The following is all based on personal preference of what to do, so don't take anything to heart. Just a heads up, I have never done inventory scripting before so don't quote me on anything, I am just providing an idea that could hypothetically work.

    Before I would start changing stuff I would make the grid into a jaggard array, and reference off that.

    I would iterate through the grid array, and combine the numbers into a string and reference that against a combinations database.

    For each cell in the grid, check if the cell contains any data, if it doesn't equal it to zero. If it does use its ID number in the string. Then reference this string against the items combinationString within the combinationDatabase.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.  
    8.     public List<string> stringArray = new List<string>();
    9.     public string combinationString;
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         for (int x = 0; x < grid[].lenght; x++) {
    14.             for (int y = 0; y < grid[][].lenght; y++) {
    15.                 //Any grid that has no data will have an ID of 0
    16.                 stringArray.Add(grid[x][y].data.id.ToString());
    17.             }
    18.         }
    19.         combinationString = stringArray.ToString();
    20.         //
    21.         for (int i = 0; i < combinationDatabase[].lenght; i++) {
    22.             if (combinationDatabase[i].combinationID == combinationString) {
    23.                 player.inventory.add(combinationDatabase[i].item);
    24.             }
    25.         }
    26.     }
    27. }
    28.  
    Minecraft terms now... Your grid will look like the grid below. This would hypothetically be for a pickaxe
    The ID of cobblestone would be 21
    The ID of stick would be 4
    The ID of empty cells that aren't used are 0

    21 21 21
    0 4 0
    0 4 0

    The grid is read starting the direction of the arrow and down.

    -> 21 21 21
    0 4 0
    0 4 0
    So your combinationString will look like: 212121040040
    You would then reference this against your combinationDatabase[].ID number, in an if statement. As seen above and here:

    Code (CSharp):
    1. for (int i = 0; i < combinationDatabase[].lenght; i++) {
    2.     if (combinationDatabase[i].combinationID == combinationString) {
    3.         player.inventory.add(combinationDatabase[i].item);
    4.     }
    5. }
    To show you what it would look like with the values inside:

    Code (CSharp):
    1. for (int i = 0; i < combinationDatabase[].lenght; i++) {
    2.     if (Pickaxe ID(212121040040) == The Objects you put in the grid(212121040040)) {
    3.         player.inventory.add(combinationDatabase[i].item(Pickaxe));
    4.     }
    5. }
    Your combinationDatabase would look like this

    Code (CSharp):
    1. public class combinationDatabase {
    2.     public GameObject item;
    3.     public string ID;
    4.     public string combinationID;
    5.     public string description;
    6.     public int cost;
    7. }

    Sorry if this was poorly describe, just keep in mind this is an idea of how you could go about this.
     
    Last edited: Jul 14, 2014
    GregMeach likes this.
  7. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    That is correct.

    I've been reading alot of information on crafting systems and alot of people mention recipe scripts ect. but no one really talks about how they check that only one item will be crafted.

    I like your post, but to be honest. its 6am and its like 5 degrees here and it kind of went over my head. I will take you're advise and try and see if i can clean up my code a bit more.

    As i'm only just getting into writting full on scripts i feel I might be doing it totally wrong, but i guess thats the best way to learn. do it one way that works even if bugs then keep trying till perfection :p

    thank you all for the reply's i'll post back here when i get something new :)
     
  8. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    Sorry to be a dumba** here, I can see the code, I just can't get my head around how i would Implement this.
    I have my Crafting Slots set up Like so.

    Code (CSharp):
    1.  
    2.       int i = 0;
    3.         for (int y=0; y < CraftingSlotsY; y++)
    4.         {
    5.             for (int x=0; x < CraftingSlotsX; x++)
    6.             {
    7.                 Rect CraftGrid = new Rect(x * 50, y * 50, 50, 50);
    8.                 GUI.Box (new Rect (x * 50, y * 50, 50, 50), y.ToString (), skin.GetStyle ("Slot"));
    9.                 Cslots [i] = Cinventory [i];
    10.                 if (Cslots [i].itemName != null) {
    11.                     GUI.DrawTexture (CraftGrid, Cslots [i].itemIcon);
    12.  
    The rest of the code is simply Mouse over etcetera for Tool tips, click and drag.

    With your Idea you are saying to Check the grid.
    My Grid slots are Simply Cslots (Crafting Slots) is that what you are referring to with the grid[].length?

    My Crafting Slots are a list. so with your method I should basically compare my list to the combination database and if it matches then allow me to "OutPut" that Item?

    I'm sorry if I have not fully understood you and i have every thing wrong but is what i said above correct?
    Regards,
    Jamie
     
  9. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    With the grid slots, I am referring to your crafting slots. And the rest sounds the same as my understanding. Tomorrow afternoon, ill rewrite the code you posted above with some additions.
     
  10. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    I hope you can have better luck then I.
    I have been starring at this code for days and all i get is errors after errors.
    I Understand Exactly what it is I need the code to do, But i believe i might be writing it completely wrong :\
    I have tried to create a new list<int> called Grid and have made it equal to Cslots[].itemID but it then gives me an error telling me " Type `int' does not contain a definition for `itemID' and no extension method `itemID' of type `int' could be found" so i think i'm just messing it up completely every time i try to get it to work.
     
  11. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Using your original code:
    You were on the right track, I tried to implement my example into your script. But the results may not be all correct depending on your Cslots, Cinventory classes in particular. Not all of the script is correct as you can see, so don't copy it straight :p

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7.    public ItemDatabase itemDB;
    8.    //
    9.    public List<string> stringArray = new List<string>();
    10.    public string combinationString;
    11.  
    12.    public int i = 0;
    13.     void Update () {    
    14.    
    15.         for (int y = 0; y < CraftingSlotsY; y++) {
    16.             for (int x = 0; x < CraftingSlotsX; x++) {
    17.                 Rect CraftGrid = new Rect(x * 50, y * 50, 50, 50);
    18.                 GUI.Box (new Rect (x * 50, y * 50, 50, 50), y.ToString (), skin.GetStyle ("Slot"));
    19.                 Cslots[i] = Cinventory[i];
    20.                 if (Cslots[i].itemName != null) {
    21.                     GUI.DrawTexture (CraftGrid, Cslots [i].itemIcon);
    22.                 }
    23.             }
    24.         }
    25.         if (GUI.Button(new Rect((raftingSlotsX * 50) + 10, (raftingSlotsY * 50) + 10, 100, 60), "Craft!")) {
    26.         int z = 0;
    27.             for (int y = 0; y < CraftingSlotsY; y++) {
    28.                 for (int x = 0; x < CraftingSlotsX; x++) {
    29.                     if (Cslots[z].itemName != null) {
    30.                         stringArray.Add(Cslots[i].ID);
    31.                         Cslots[z].remove;
    32.                     }
    33.                     else stringArray.Add("0");
    34.                 }
    35.             }
    36.             combinationString = stringArray.ToString();
    37.             for (int z = 250; z < itemDB.items.Length; z++) {
    38.                 if (itemDB.items[z].combinationID == combinationString) {
    39.                     Cinventory[].add(itemDB.items[z]);
    40.                 }
    41.             }
    42.         }
    43.     }
    44. }
    45.  
    This would be another script holding all of your items in the game.

    Code (CSharp):
    1.  
    2. public class ItemDatabase : ScriptableObject {
    3.  
    4.     public Item[] items;
    5.  
    6.     //Items below the ID of 250 are NON-CRAFTABLE
    7.     public class Item {
    8.         public GameObject item;
    9.         public string ID;
    10.         public string combinationID;
    11.         public string description;
    12.         public int cost;
    13.     }
    14. }
    15.  
    Just keep in mind that I have never made one of these before, so I could be completely wrong about how I am going about this.
     
    Last edited: Jul 14, 2014
  12. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    Love your work there mate, I appreciate you trying to help out. I made a copy of my script (Just to make sure i have it because i might have been on the right track with it) & I have started to implement your changes and it was only giving me one error. I fixed that error and more showed up..
    I had to changed:
    Code (CSharp):
    1.  
    2. stringArray.Add(Cslots[i].itemID);
    3. //too
    4. stringArray.Add(""+Cslots[i].itemID);
    5.  
    and then i was faced with this...

    Code (CSharp):
    1.  
    2. Cslots[z].remove; //(87,65): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    3.  
    and it is also showing ".Add" in red so i'm going to make a guess at Unity doesn't like .Add or .Remove
    I also get rrors for every where you have used .Count

    Code (CSharp):
    1.  
    2. for (int j = 0; j <database.items.Count; j++)
    3. // error CS0176: Static member `ItemDatabase.items' cannot be accessed with an instance reference, qualify it with a type name instead.
    4.  
    5. Cinventory[].add(ItemDatabase.items[k]);
    6.  
    7. //error CS0118: `Inventory.Cinventory' is a `field' but a `type' was expected.
    8.  
    9. for (int k = 250; k < ItemDatabase.items.Length; k++)
    10.  
    11. //error CS1061: Type `System.Collections.Generic.List<Item>' does not contain a definition for `Length' and no extension method `Length' of type `System.Collections.Generic.List<Item>' could be found (are you missing a using directive or an assembly reference?)
    12.  
    This is one hell of a head ache for me. :S I think i might have been on the right track with how i was doing it. but i'm not sure as i need to beable to see the Data it is creating/Storing in stringArray / CombinationString. If i use a debug.Log it just says "String Array" To know if mine is working i need it to show me its actual data.

    I will keep playing with your script here and mine. but I know i will probably get sick and tired of looking at it soon haha.
     
  13. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Code (CSharp):
    1. Cslots[z].remove;
    This wasn't supposed to work the first instance :p but just to show that you needed to delete the item. Use your own Cslots remove method to delete them :)

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class ItemDatabase : ScriptableObject {
    4.  
    5.     public Item[] items;
    6.  
    7.     //Items below the ID of 250 are NON-CRAFTABLE
    8.     [System.Serializable]
    9.     public class Item {
    10.         public GameObject item;
    11.         public string ID;
    12.         public string combinationID;
    13.         public string description;
    14.         public int cost;
    15.     }
    16. }
    17.  
    Try that. have a look at THIS it shows the way to use ScriptableObject.
     
  14. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    @djfunkey ,
    Thanks for that link mate. I tried watching it last night but managed to fall asleep... :|

    I'll be having a look again tonight after
    i finish work (1hr 49mins) and see what i can do. :)
     
  15. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    Thank you for your help. I also asked a friend who is a lecturer for c#. He showed me the simple answer. Using your code (thank you muchly) instead of a list<String> stringarray.

    Its just a simple string.
    Code (CSharp):
    1. Public string stringarray;
    2.  
    I can't believe I didn't even try this.
    And as for the combinationstring, I have a combostring on every item in my item list. And I watched the video you linked, thank you for that also. I made changes to my inspector from his tutorial.

    Regards,
    Jamie.
     
  16. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    If you don't mind could you post or send through the final code as so other people and I can see what the solution was, because I have no idea if I even planned it out the right way. @GingerNinja04
     
  17. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    Alrighty.. So Lets Try To Explain this script In Parts as I don't want to give out every part of my script. so i'll try to share the Drawing Crafting Grid, And The Basic Craft Button.

    So First of we Have the Item Script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Item {
    6.     public int itemID;
    7.     public string itemName;
    8.     public string itemDesc;
    9.     public Texture2D itemIcon;
    10.     public int itemPower;
    11.     public ItemType itemType;
    12.     public string combinationID;
    13.  
    14.     public enum ItemType {
    15.         Weapon,
    16.         Consumable,
    17.         Misc
    18.     }
    19.  
    20.     public Item(int Id, string Name, string Desc, int Power, int Speed, string comboID, ItemType Type)
    21.     {
    22.         itemID = Id;
    23.         itemName = Name;
    24.         itemDesc = Desc;
    25.         itemIcon = Resources.Load<Texture2D>("Item Icons/" +Name);
    26.         itemPower = Power;
    27.         combinationID = comboID;
    28.         itemType = Type;
    29.     }
    30. //Blank Item If Inventory slot is empty This Item Fills that Slot.
    31.     public Item()
    32.     {
    33.         itemID = 0;
    34.     }
    35. }

    And this is the Inventory Script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.     public class Inventory : MonoBehaviour {
    6.  
    7.     public int CraftingSlotsX, CraftingSlotsY;
    8.     public int slotsX, slotsY;
    9.     static public List<Item> inventory = new List<Item>();
    10.     static public List<Item> Cinventory = new List<Item>();
    11.     static public ItemDatabase database;
    12.     List<Item> slots = new List<Item>();
    13.     List<Item> Cslots = new List<Item>();
    14.     private bool showInventory;
    15.     public GUISkin skin;
    16.     private bool draggingItem;
    17.     private Item draggedItem;
    18.     private int prevIndex;
    19.     private int Boxdimension = 50;
    20.  
    21.     // This Fills the inventory/Craft Grid with Empty Items and AddItem(); is used to Give items to player on Spawn.
    22.     void Start ()
    23.     {
    24.         for (int i = 0; i < (CraftingSlotsX * CraftingSlotsY); i++) {
    25.         Cslots.Add (new Item());
    26.         Cinventory.Add (new Item());
    27.     }
    28.         database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
    29.         AddItem (2);
    30. }
    31. //Inventory is Set up under Edit>Project Settings>Input
    32.     void Update()
    33.     {          
    34.         if (Input.GetButtonDown ("Inventory"))
    35.         {
    36.             showInventory = !showInventory;
    37.         }
    38.  
    39.     }
    40. //Calls the Drawing of all GUI Aspects.
    41.     void OnGUI()
    42.     {
    43.  
    44.         DrawHotBar ();
    45.         GUI.skin = skin;
    46.             if (showInventory)
    47.             {
    48.                 DrawCraftingArea ();
    49.                 DrawInventory();
    50.             if (draggingItem)
    51.                 {
    52.                 GUI.Box (new Rect(Event.current.mousePosition.x +15f, Event.current.mousePosition.y, 50,50), draggedItem.itemIcon);
    53.                 }
    54.             }
    55.     }
    56.  
    57.     //Draws Inventory and allows basic Click and Drag on items.
    58.     void DrawInventory()
    59.     {
    60.                 Event e = Event.current;
    61.                 int i = 0;
    62.                 for (int y=0; y < slotsY; y++) {
    63.                         for (int x=0; x < slotsX; x++)
    64.             {  
    65.                 //Rect slotRect = new Rect (x * 50,y* 50, 50, 50);
    66.                 //GUI.Box (new Rect (x * 50, y * 50, 50, 50), y.ToString (), skin.GetStyle ("Slot"));
    67.                 Rect slotRect = new Rect (((Screen.width/2)-((slotsX *(Boxdimension+10))/2))+(x * 60),((Screen.height/2)+((slotsY *(Boxdimension+10))/2))-(y*60),Boxdimension,Boxdimension);
    68.                 GUI.Box(new Rect(((Screen.width/2)-((slotsX *(Boxdimension+10))/2))+(x * 60),((Screen.height/2)+((slotsY *(Boxdimension+10))/2))-(y*60),Boxdimension,Boxdimension), y.ToString(), skin.GetStyle("Slot"));                      
    69.  
    70.                 slots [i] = inventory [i];
    71.                                         if (slots [i].itemName != null) {
    72.                                                 GUI.DrawTexture (slotRect, slots [i].itemIcon);
    73.                                                 if (slotRect.Contains (e.mousePosition)) {
    74.                                                         tooltip = CreateTooltip (slots [i]);
    75.                                                         showTooltip = true;
    76.  
    77.                                                         if (e.button == 0 && e.type == EventType.mouseDrag && !draggingItem) {
    78.                                                                 draggingItem = true;
    79.                                                                 prevIndex = i;
    80.                                                                 draggedItem = slots [i];
    81.                                                                 inventory [i] = new Item ();
    82.                                
    83.                                                         }
    84.                                                         if (e.type == EventType.mouseUp && draggingItem) {
    85.                                                                 inventory [prevIndex] = inventory [i];
    86.                                                                 inventory [i] = draggedItem;
    87.                                                                 draggingItem = false;
    88.                                                                 draggedItem = null;
    89.                                                         }
    90.                                                 }
    91.                
    92.                                         } else {
    93.                                                 if (slotRect.Contains (e.mousePosition)) {
    94.                                                         if (e.type == EventType.mouseUp && draggingItem) {
    95.                                                                 inventory [i] = draggedItem;
    96.                                                                 draggingItem = false;
    97.                                                                 draggedItem = null;
    98.                                                         }
    99.                                                 }
    100.                                         }
    101.  
    102.                                         if (tooltip == "") {
    103.                                                 showTooltip = false;
    104.                                         }
    105.  
    106.                                         i++;
    107.                                 }
    108.  
    109.                         }
    110.                 }
    111.        
    112.     void DrawCraftingArea()
    113.     {
    114.         if (GUI.Button (new Rect (150, 50, 60, 50), "Craft"))
    115.         {
    116.             CraftItem();
    117.         }
    118.             //Draws Crafting Grid To be What ever size you specify in the Inspector. And each Slot in the Grid is Referred to as CSlots and items are stored into the CInventory (Crafting Inventory)
    119.         Event e = Event.current;
    120.         int i = 0;
    121.         for (int y=0; y < CraftingSlotsY; y++)
    122.         {
    123.             for (int x=0; x < CraftingSlotsX; x++)
    124.             {
    125.                 Rect CraftGrid = new Rect(x * 50, y * 50, 50, 50);
    126.                 GUI.Box (new Rect (x * 50, y * 50, 50, 50), y.ToString (), skin.GetStyle ("Slot"));
    127.                 Cslots [i] = Cinventory [i];
    128.                 if (Cslots [i].itemName != null) {
    129.                     GUI.DrawTexture (CraftGrid, Cslots [i].itemIcon);
    130.                     if (CraftGrid.Contains (e.mousePosition)) {
    131.                         tooltip = CreateTooltip (Cslots [i]);
    132.                         showTooltip = true;
    133.                        
    134.                         if (e.button == 0 && e.type == EventType.mouseDrag && !draggingItem) {
    135.                             draggingItem = true;
    136.                             prevIndex = i;
    137.                             draggedItem = Cslots [i];
    138.                             Cinventory [i] = new Item ();
    139.                            
    140.                         }
    141.                         if (e.type == EventType.mouseUp && draggingItem) {
    142.                             Cinventory [prevIndex] = Cinventory [i];
    143.                             Cinventory [i] = draggedItem;
    144.                             draggingItem = false;
    145.                             draggedItem = null;
    146.                         }
    147.                     }
    148.                    
    149.                 } else {
    150.                     if (CraftGrid.Contains (e.mousePosition)) {
    151.                         if (e.type == EventType.mouseUp && draggingItem) {
    152.                             Cinventory [i] = draggedItem;
    153.                             draggingItem = false;
    154.                             draggedItem = null;
    155.                         }
    156.                     }
    157.                 }
    158.                
    159.                 if (tooltip == "") {
    160.                     showTooltip = false;
    161.                 }
    162.                
    163.                 i++;
    164.             }
    165.            
    166.         }
    167.     }
    168.  
    169. // Compares Crafting grid Slots To the Combination String Of items To see if An Item Can be Crafted.
    170. void CraftItem()
    171. {
    172.     string itemsStr = "";
    173.     for (int i=0; i < Cslots.Count; i++){
    174.         itemsStr += Cslots[i].itemID.ToString();
    175.     }
    176.     Debug.Log(itemsStr);
    177.     for (int i=0; i < database.items.Count; i++){
    178.         if(database.items[i].combinationID == itemsStr) {
    179.             AddItem(database.items[i].itemID);
    180.                 RemoveCraft();
    181. }
    182.  
    183.  
    184. }
    185.     }
    186. //Removes & Perm Deletes Items From CraftingGrid
    187.     void RemoveCraft()
    188.     {
    189.         for (int i = 0; i < Cinventory.Count; i++)
    190.             if (Cinventory[i].itemID != 0)
    191.         {
    192.             Cinventory[i] = new Item();
    193.         }
    194.        
    195.     }
    196. }
    Its very simple to understand really

    I hope Any one reading Can Follow. If any one needs A Better Explanation Please Comment or PM me and I will Help you out :)
    Regards,
    Jamie