- Home /
 
Inventory Drop Function Problem
Hello All, My name is anthony faraci and im in need of a bit of help...Ive been programing and scripting for 5 almost 6 years, but im having a bit of a problem with my inventory system. I have the inventory itself working and the pick up of items, but i cant seem to figure out how I could drop the items. Here are my Scripts:
This is attached to the Item itself:
 public var InventoryIcon :  Texture2D;
 public var ItemDescription : String;
 public var ItemClass : String;
 public var defence : int;
 public var damage : int;
 
 var doWindow : boolean = false;
 
 function OnMouseOver() {
 doWindow=true;
 }
 function OnMouseExit() {
 doWindow=false;
 }
 
 function OnGUI() {
 
 if (doWindow)
 GUI.Window (0, Rect (110,10,200,80), DoWindow, "Info");
 }
 
 function OnMouseDown() {
     var inv = FindObjectOfType(inv2);
     inv.AddItem( gameObject, InventoryIcon );
     Network.Destroy(this.gameObject);
     Debug.Log("item picked");
 }
 
 function DoWindow (windowID : int) {
     GUI.Label (new Rect(5, 15, 200, 25), "Item:" + ItemDescription);
     GUI.Label (new Rect(5, 30, 90, 25), "Class:" + ItemClass);
     GUI.Label (new Rect(5, 45, 90, 25), "Defence:" + defence);
     GUI.Label (new Rect(5, 60, 90, 25), "Damage:" + damage);
 }
 
               This is Put on the player:
 var inventory : Array;
 
 var doWindow : boolean = false;
 
 
 public var emptyTex : Texture;
 
 
 public var inventorySizeX = 4;
 public var inventorySizeY = 5;
 
 var iconWidthHeight = 40;
 var spacing = 4;
 
 public var offSet = Vector2( 100, 100 );
 
 private var itemImage : Texture2D;
 
 
 class InventoryItem
 {
    var worldObject : GameObject;
    var texRepresentation : Texture2D;
 }
 
 function Awake()
 {
    inventory = new Array(inventorySizeX);
    
     for( var i = 0; i < inventory.length; i ++ )
     {
         inventory[i] = new Array(inventorySizeY);
     }
 }
 
 
 function OnGUI() {
 
    var texToUse : Texture2D;
    var currentInventoryItem : InventoryItem;
 
     for( var i = 0; i < inventory.length; i ++ )
     {
         for( var k = 0; k < inventory[i].length; k ++ )
         {
            texToUse = emptyTex;
            currentInventoryItem = inventory[i][k];
            
             if( inventory[i][k] != null )
             {
                 texToUse = currentInventoryItem.texRepresentation;
             }
            
             var it = GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
             GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
             var w : int = 0;
             var h : int = 0;
             if (it){//&& (inventory[i][k]==!null){
 
                 //This is were the drop item command would go
             }
         }
     }
 }
 
 function AddItem( item : InventoryItem )
 {
     for( var i = 0; i < inventory.length; i ++ )
     {
         for( var k = 0; k < inventory[i].length; k ++ )
         {
            if( inventory[i][k] == null )
             {
                 inventory[i][k] = item;
                 return;
             }
         }
     }   
 }
 
 function AddItem( worldObject : GameObject, texRep : Texture2D )
 {
    var newItem = new InventoryItem();
 
     newItem.worldObject = worldObject;
     newItem.texRepresentation = texRep;
       
    AddItem( newItem );   
 }
 
               Any help would be much appreciated!
tl;dr. You would do better to explain the problem, and how you have attempted to solve it in words or pseudocode, rather than posting a wall of code. I did skim your code and from what I can see it seems you want to drop items when you update the GUI, that seems incorrect.
Answer by slkjdfv · Jun 10, 2011 at 05:17 AM
If you'd look at the showcase forum there are better inventory systems that have item pickup and dropping. try this system out i made it myself and its very easy to use http://forum.unity3d.com/threads/90871-advanced-inventory-system-example
Your answer
 
             Follow this Question
Related Questions
How to optimize this script and add items imediately without grids 0 Answers
Having scripts interact 1 Answer
Inventory code not working [JS] 0 Answers
Is this considered bad coding 1 Answer