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