need some scripting help

Hi, I’m currently working an a first person role playing game and at the moment I’m working on an inventory system. I currently have a very basic one thanks to a thread which I can’t seem to find anymore sadly. But there seems to be a problem with it, when I left click an item it drops the item like it’s supposed to… but if I right click the item the item stays in my inventory and drops a clone if the item.

Here is the script:

static var statInventory : Inventory;

//Our inventory
var inventory : Array;

//This will be drawn when a slot is empty
public var emptyTex : Texture;

//the size of the inventory in x and y dimension
public var inventorySizeX = 8;
public var inventorySizeY = 5;

//The pixel size (height and width) of an inventory slot
var iconWidthHeight = 20;

//Space between slots (in x and y)
var spacing = 4;

//set the position of the inventory
public var offSet = Vector2( 100, 100 );

// TEST VARIABLES
// Assign these to test adding Items
public var testTex : Texture;
public var testTex2 : Texture;

public var testInvObject : GameObject;
public var testInvObject2 : GameObject;

//Our Representation of an InventoryItem
@System.Serializable
class InventoryItem
{
   //GameObject this item refers to
   var worldObject : GameObject;
   //What the item will look like in the inventory
   var texRepresentation : Texture;
}

// Create the Inventory
function Awake()
{
	statInventory = this;
	
   inventory = new Array(inventorySizeX);
   
    for( var i = 0; i < inventory.length; i ++ )
    {
        inventory[i] = new Array(inventorySizeY);
    }
}

function OnGUI()
{
    var texToUse : Texture;
    var currentInventoryItem : InventoryItem;
   
     //Go through each row
     for( var i = 0; i < inventory.length; i ++ )
     {
         // and each column
         for( var k = 0; k < inventory[i].length; k ++ )
         {
             currentInventoryItem = inventory[i][k];
           
             //if there is an item in the i-th row and the k-th column, draw it
             if( inventory[i][k] == null )
             {
                 GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
             }
             else
             {
             	GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
             }
             
           	 if(currentInventoryItem != null  
           	    GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", GUIStyle("label") ))
             {
             	
             	currentInventoryItem.worldObject.transform.position = transform.position;
             	currentInventoryItem.worldObject.transform.rotation = transform.rotation;
             	currentInventoryItem.worldObject.active = true;
             	
             	if(Input.GetMouseButtonUp(0))
             	{		
             		//Equip it
             		currentInventoryItem.worldObject.transform.parent = transform;
             		
             	} else if(Input.GetMouseButtonUp(1))
             	{
             		//Drop it
             		inventory[i][k] = null;	
             		currentInventoryItem.worldObject.transform.parent = null;
      
             	}
             }
         }
    }
    
    if( GUILayout.Button("AddItem1"))
    {
    	var newInvObj = Instantiate(testInvObject, Vector3.zero, Quaternion.identity);
    	newInvObj.active = false;
        AddItem( newInvObj, testTex );   
    }
   
    if( GUILayout.Button("AddItem2"))
    {
        var newInvObj2 = Instantiate(testInvObject2, Vector3.zero, Quaternion.identity);
    	newInvObj2.active = false;
        AddItem( newInvObj2, testTex2 );      
    }   
}

function AddItem( item : InventoryItem )
{
    //Go through each row
    for( var i = 0; i < inventory.length; i ++ )
    {
        // and each column
        for( var k = 0; k < inventory[i].length; k ++ )
        {
           //If the position is empty, add the new item and exit the function
           if( inventory[i][k] == null )
            {
                inventory[i][k] = item;
                return;
            }
        }
    }   
   
    //If we got this far, the inventory is full, do somethign appropriate here   
}

function AddItem( worldObject : GameObject, texRep : Texture )
{
   var newItem = new InventoryItem();
   
   newItem.worldObject = worldObject;
   newItem.texRepresentation = texRep;
      
   AddItem( newItem );   
}

and here is the code for the item:

public var iconTex : Texture;

function OnMouseDown()
{
	gameObject.active = false;
	
	var item = new InventoryItem();
	item.worldObject = gameObject;
	item.texRepresentation = iconTex;
	Inventory.statInventory.AddItem(item);
}

at the moment I can pick up items by clicking them with the mouse… but it’s very hard to target it since my camera moves with the mouse and I can pickup the item from any distance. dose anyone have any tips or know a way to make a sort of target in the center of the screen (like a crosshair) where if it is over the item and the player is within a certain distance from the item then the player picks up the item.

I’m not very good at scripting, but I’m hoping that if I ask around the unity forums, I will eventually start to understand it more and more.

After the condition if(Input.GetMouseButtonUp(0))
You could put inventory*[k] = null; To clear the array when you equip it. Cause when you left click you equip it, if you see it falling its cause its rigidbody needs to be kinematic and find its parents vactor3*
* *inventory[i][k] currentInventoryItem.worldObject.transform.parent = transform; currentInventoryItem.worldObject.transform.localPosition = Vector3(0, 0, 0); currentInventoryItem.worldObject.rigidbody.isKinematic = true; currentInventoryItem.worldObject.active = true;* *
To fix the right click thing you may want to look into those lines in there that are adding the items and comment them out to find what is still creating it :smile:
To make my cam stay still while i target something i use a pause script. There is one in the fps demo, and alter it so you can enable and disable a few scripts of your choice(ie)camera movement. It show you how to lock your cursor ann enable a crosshair in the center where your cursor locks to.

I tried what you suggested and it works but there are 2 problems that I can’t seem to figure out: I managed to make left click drop the item, but when I drop the item it follows the player around, I was also wondering how do I increase the range at which the player can pick up items.

here is the updated part of the code:

             	if(Input.GetMouseButtonUp(0))
             	{		
             		inventory[i][k] = null;
					currentInventoryItem.worldObject.transform.parent = null; 
					currentInventoryItem.worldObject.transform.localPosition = Vector3(0, 0, 0); 
					currentInventoryItem.worldObject.rigidbody.isKinematic = false; 
					currentInventoryItem.worldObject.active = true; 
             		
				} //else if(Input.GetMouseButtonUp(1))
             	
             		//Drop it
             		//inventory[i][k] = null;	
             		//currentInventoryItem.worldObject.transform.parent = null;
             }

Thanks again for the help.

ok, so the script seems to be working now, the item is drooping properly.

I’ve also looked through the fps tutorial and edited the “LockCursor” script. It seems to be working fine, but I can’t seem to figure out a way to pause the script when I press a certain key, that way causing it too free the mouse, then when I press the key again the script resumes.

Here is the script:

function OnMouseDown ()
{
	{
		// Lock the cursor
		Screen.lockCursor =	true;
	}

	private var wasLocked = false;

	function Update ()
	{
		if (Input.GetMouseButton(0))
			Screen.lockCursor = true;
	
		// Did we lose cursor locking?
		// eg. because the user pressed escape
		// or because he switched to another application
		// or because some script set Screen.lockCursor = false;
		if (!Screen.lockCursor  wasLocked)
		{
			wasLocked = false;
		}
		// Did we gain cursor locking?
		else if (Screen.lockCursor  !wasLocked)
		{
			wasLocked = true;
		}
	}
}

There also seems to be an error with the first “Screen.LockCursor = true;” it say’s it expects to find a “:” instead of an “=” but when I change it I get a whole bunch of errors, and if I leave the “=” the script works like it’s supposed to.

You seem to have got the Update function inside the OnMouseDown function, which is not allowed. If you remove the first and last curly brace from the file, you should be OK.