- Home /
My inventory system only picks up one item when there are several
I spent hours writing this Inventory script and I'm sure there are easier ways to do it. I'm using a trigger on both the player and the "bandage" item. it works flawlessly until I try to pick up multiple bandages. No matter how many are there, it just picks up one. I understand why, I just can't find a solution for it. Any help would be most appreciated.
var invIsOpen : boolean = false;
var Player : GameObject;
var MainCamera : GameObject;
var numOfBandages : int = 0;
var numOfPAmmo : int = 0;
var bandagePrefab : Transform;
var pistolAmmoPrefab : Transform;
var pickUpMessage : boolean = false;
var isBandage : boolean = false;
var isPAmmo : boolean = false;
var invWeight : float = 0.0f;
var canPickupMore : boolean = true;
var windowRect : Rect = Rect (20, 20, 500, 500);
function Update()
{
//pickup restraints
if(invWeight == 100.0)
{
canPickupMore = false;
}
//Pickup Setup
if(pickUpMessage == true)
{
if(isBandage == true)
{
if(canPickupMore == true)
{
if(Input.GetKey(KeyCode.F))
{
numOfBandages += 1;
invWeight += 0.2f;
isBandage = false;
pickUpMessage = false;
}
}
}
}
if(Input.GetKey(KeyCode.E))
{
invIsOpen = true;
}
if(Input.GetKey(KeyCode.Escape))
{
invIsOpen = false;
}
if(invIsOpen == true)
{
Player.GetComponent(MouseLook).enabled = false;
}
else
{
Player.GetComponent(MouseLook).enabled = true;
}
}
function OnTriggerEnter(itemEnt : Collider)
{
if(itemEnt.gameObject.tag == "Bandage")
{
pickUpMessage = true;
isBandage = true;
}
}
function OnTriggerExit(itemEx : Collider)
{
pickUpMessage = false;
isBandage = false;
}
function OnGUI()
{
if(pickUpMessage == true)
{
GUI.Label(Rect(Screen.width/2, Screen.height/1.5, 200,50), "Press 'F' to pickup");
}
if(invIsOpen == true)
{
windowRect = GUI.Window (0, windowRect, DoMyWindow, "Inventory");
}
}
////////////////////
//INVENTORY WINDOW//
////////////////////
function DoMyWindow (windowID : int)
{
//Inventory Weight
GUI.Label(Rect(210, 20, 100, 50), "Weight(kg): " +invWeight);
GUI.DragWindow (Rect (0,0, 10000, 20));
//Bandages
GUI.Label(Rect(10, 50, 100, 50), "Bandages: " +numOfBandages);
if(numOfBandages > 0)
{
if(GUI.Button(Rect(120, 50, 50, 20), "Drop 1"))
{
numOfBandages += -1;
var dropBandage = Instantiate(bandagePrefab, GameObject.Find("Drop Point").transform.position, Quaternion.identity);
dropBandage.rigidbody.AddForce(transform.forward * 2);
}
if(GUI.Button(Rect(180, 50, 50, 20), "Use 1"))
{
numOfBandages += -1;
}
}
//Pistol Ammo
GUI.Label(Rect(10, 70, 100, 50), "Pistol Ammo: " +numOfPAmmo);
if(numOfPAmmo > 0)
{
if(GUI.Button(Rect(120, 50, 50, 20), "Drop 1"))
{
numOfPAmmo += -1;
var dropPammo = Instantiate(pistolAmmoPrefab, GameObject.Find("Drop Point").transform.position, Quaternion.identity);
dropPammo.rigidbody.AddForce(transform.forward * 2);
}
}
}
funny, $$anonymous$$e picks up all item nearby. This was my approach. I cached all items in the scene. Then, I created a function to return a list of items within player "pickup-range", when that was met put that item into the list nearbyItems. Then when the time came and the player wanted to pick up the items, I would traverse the list and put all items from nearbyItems into my inventory.
Answer by tw1st3d · Jul 25, 2013 at 03:37 AM
First off, good god you really need to format this better. Anyway, this should solve your problems. Handled your ifs better and added in a conditional on canPickupMore for true/false
var Player : GameObject;
var MainCamera : GameObject;
var numOfBandages : int = 0;
var numOfPAmmo : int = 0;
var bandagePrefab : Transform;
var pistolAmmoPrefab : Transform;
var pickUpMessage : boolean = false;
var isBandage : boolean = false;
var isPAmmo : boolean = false;
var invIsOpen : boolean = false;
var invWeight : float = 0.0f;
var canPickupMore : boolean = true;
var windowRect : Rect = Rect (20, 20, 500, 500);
function Update()
{
if(invWeight == 100.0)
canPickupMore = false;
else
canPickupMore = true;
if(pickUpMessage == true && isBandage == true)
{
if(canPickupMore == true && Input.GetKey(KeyCode.F))
{
numOfBandages += 1;
invWeight += 0.2f;
isBandage = false;
pickUpMessage = false;
}
}
if(Input.GetKey(KeyCode.E))
invIsOpen = true;
if(Input.GetKey(KeyCode.Escape))
invIsOpen = false;
if(invIsOpen == true)
Player.GetComponent(MouseLook).enabled = false;
else
Player.GetComponent(MouseLook).enabled = true;
}
function OnTriggerEnter(itemEnt : Collider)
{
if(itemEnt.gameObject.tag == "Bandage")
{
pickUpMessage = true;
isBandage = true;
}
}
function OnTriggerExit(itemEx : Collider)
{
if(pickUpMessage)
{
pickUpMessage = false;
isBandage = false;
}
}
function OnGUI()
{
if(pickUpMessage == true)
GUI.Label(Rect(Screen.width/2, Screen.height/1.5, 200,50), "Press 'F' to pickup");
if(invIsOpen == true)
windowRect = GUI.Window (0, windowRect, DoMyWindow, "Inventory");
}
function DoMyWindow (windowID : int)
{
GUI.Label(Rect(210, 20, 100, 50), "Weight(kg): " +invWeight);
GUI.DragWindow(Rect (0,0, 10000, 20));
GUI.Label(Rect(10, 50, 100, 50), "Bandages: " +numOfBandages);
if(numOfBandages > 0)
{
if(GUI.Button(Rect(120, 50, 50, 20), "Drop 1"))
{
numOfBandages += -1;
var dropBandage = Instantiate(bandagePrefab, GameObject.Find("Drop Point").transform.position, Quaternion.identity);
dropBandage.rigidbody.AddForce(transform.forward * 2);
}
if(GUI.Button(Rect(180, 50, 50, 20), "Use 1"))
numOfBandages += -1;
}
GUI.Label(Rect(10, 70, 100, 50), "Pistol Ammo: " +numOfPAmmo);
if(numOfPAmmo > 0)
{
if(GUI.Button(Rect(120, 50, 50, 20), "Drop 1"))
{
numOfPAmmo += -1;
var dropPammo = Instantiate(pistolAmmoPrefab, GameObject.Find("Drop Point").transform.position, Quaternion.identity);
dropPammo.rigidbody.AddForce(transform.forward * 2);
}
}
}
I cant thank you enough :D Yes I know, my organization is terrible :/
I copied in the changes and it didn't really fix the problem, Im still only picking up one bandage when there are three on the ground. :(
Is it supposed to be picking up all three all at once, or is it only letting you pick one up of the three that are there and then it locks?
Your answer