- Home /
need help deleting ammo item in inventory on gun reload
Hello Everyone,
I am currently adding the finishing touches for a gun script and seem to have run into a bit of a snag. I have it that ammo is added in a GUI inventory system. Once the ammo item is picked up, it adds a +1 to the players total Ammo amount (int). Once the player reloads the gun the total Ammo count goes down, but the Ammo in the GUI inventory is still there.
How can I get it that once I hit the "R" key to reload, it destroys so-and-so gun ammo from the inventory system? For some reason, I just can't get my brain around it today.
Here is the code for the shoot/reload function on the gun ( I took out the FireHandgun function)
var bulletType: Rigidbody;
var bulletSpeed = 10;
private var handgunROF = 0.5;
var gunIsHeld : boolean = false;
var playerAmmoCount:ammoCount;
private var handgunFire :boolean = true;
function Update ()
{
FireHandgun();
ReloadHandgun();
}
function ReloadHandgun()
{
if (Input.GetKeyDown("r"))
{
yield WaitForSeconds(playerAmmoCount.reloadtime);
if (playerAmmoCount.roundsRevolverAmmo <6)
{
playerAmmoCount.roundsInRevolver += playerAmmoCount.roundsRevolverAmmo;
playerAmmoCount.roundsRevolverAmmo -= playerAmmoCount.roundsRevolverAmmo;
}
if (playerAmmoCount.roundsRevolverAmmo >6)
{
playerAmmoCount.roundsInRevolver += 6;
playerAmmoCount.roundsRevolverAmmo -= 6;
}
else
{
playerAmmoCount.roundsRevolverAmmo -=6;
}
}
}
Here is the code for the total Ammo count
var roundsInRevolver = 6; //amount of bullets in gun
var roundsRevolverAmmo = 10; //Total ammount of Ammo I have
var reloadtime = 3; //time it takes to reload the gun
function Update ()
{
if (roundsRevolverAmmo <0)
{
roundsRevolverAmmo =0;
}
if (roundsInRevolver <0)
{
roundsInRevolver =0;
}
if (roundsInRevolver >6)
{
roundsInRevolver--;
roundsRevolverAmmo++;
}
}
Would anyone be able to point me in the right direction or show me how to accomplish this? Thank you. :)
Elaborate on the GUI Inventory system: List/Array/something else? You want to find the item in your Inventory system and remove it - right? Something like Inventory.Remove(theItem)
I'm using Brackeys free inventory system. I believe it is an Array. Here is the main part of his inventory code.
var Contents : Transform[]; //The content of the Inventory
var $$anonymous$$axContent : int = 12; //The maximum number of items the Player can carry.
var Debug$$anonymous$$ode = false;
private var playersInvDisplay : InventoryDisplay; //$$anonymous$$eep track of the InventoryDisplay script.
static var itemHolderObject : Transform; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.
@script AddComponent$$anonymous$$enu ("Inventory/Inventory")
//Handle components and assign the itemHolderObject.
function Awake ()
{
itemHolderObject = gameObject.transform;
playersInvDisplay = GetComponent(InventoryDisplay);
if (playersInvDisplay == null)
{
Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
}
}
//Add an item to the inventory.
function AddItem(Item:Transform)
{
var newContents = new Array(Contents);
newContents.Add(Item);
Contents=newContents.ToBuiltin(Transform); //Array to unity builtin array
if (Debug$$anonymous$$ode)
{
Debug.Log(Item.name+" has been added to inventroy");
}
//Tell the InventoryDisplay to update the list.
if (playersInvDisplay != null)
{
playersInvDisplay.UpdateInventoryList();
}
}
//Removed an item from the inventory (IT DOESN'T DROP IT).
function RemoveItem(Item:Transform)
{
var newContents=new Array(Contents);
var index=0;
var shouldend=false;
for(var i:Transform in newContents) //Loop through the Items in the Inventory:
{
if(i == Item) //When a match is found, remove the Item.
{
newContents.RemoveAt(index);
shouldend=true;
//No need to continue running through the loop since we found our item.
}
index++;
if(shouldend) //Exit the loop
{
Contents=newContents.ToBuiltin(Transform);
if (Debug$$anonymous$$ode)
{
Debug.Log(Item.name+" has been removed from inventroy");
}
if (playersInvDisplay != null)
{
playersInvDisplay.UpdateInventoryList();
}
return;
}
}
}
//Dropping an Item from the Inventory
function DropItem(item)
{
gameObject.Send$$anonymous$$essage ("PlayDropItemSound", Send$$anonymous$$essageOptions.DontRequireReceiver); //Play sound
var makeDuplicate = false;
if (item.stack == 1) //Drop item
{
RemoveItem(item.transform);
}
else //Drop from stack
{
item.stack -= 1;
makeDuplicate = true;
}
item.Drop$$anonymous$$eFromThePlayer(makeDuplicate);
if (Debug$$anonymous$$ode)
{
Debug.Log(item.name + " has been dropped");
}
}
I've tried something like the Inventory.Remove("Ammo test") but it doesn't seem to work properly. Also how would I get it to know how many to delete. Such as I have 2 out of 6 rounds in the gun and reload, using 4, so how would it know to delete 4 "Ammo test" items?
First need to know if 'Ammo' got into Inventory, it looks like it's expecting you to call the method AddItem(Item:Transform) passing it a transform, presumambly the transform of the "ammo"
Did you do that at some point in your code? If so, show that part please.
It gets added in. It's hard to know which part of the code it happens though. As I said, this is Brackeys free inventory system, and it is in several different parts. I think I found it though in another Script called "Items"
function PickUpItem ()
{ var getit=true; if(canGet){//if its getable or hasnt been gotten.
playersinv.gameObject.Send$$anonymous$$essage ("PlayPickUpSound", Send$$anonymous$$essageOptions.DontRequireReceiver); //Play sound
if(stackable){
var locatedit:Item;
for(var t:Transform in playersinv.Contents){
if(t.name==this.transform.name){//if the item we wanna stack this on has the same name
var i:Item=t.GetComponent(Item);
if(i.stack<i.maxStack){
locatedit=i;
}
}
}
if(locatedit!=null){//if we have a stack to stack it to!
getit=false;
locatedit.stack+=1;
Destroy(this.gameObject);
}
else{
getit=true;
}
}
//If we can get it and the inventory isn't full.
if (getit && playersinv.Contents.length < playersinv.$$anonymous$$axContent)
{
playersinv.AddItem(this.transform);
$$anonymous$$ove$$anonymous$$eToThePlayer(playersinv.itemHolderObject);//moves the object, to the player
}
else if (playersinv.Contents.length >= playersinv.$$anonymous$$axContent)
{
Debug.Log("Inventory is full");
}
}
}
Answer by Therian13 · Jan 26, 2014 at 01:57 AM
I'm afraid it didn't work. I decided to go a different route. maybe I can get it to work one day. Thank you though...
Your answer
![](https://koobas.hobune.stream/wayback/20220613130739im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gun - Ammo , reloading and UI problem. 1 Answer
About Classes in js 2 Answers
Reload After Magazine Is Empty 2 Answers
Ammo Script 1 Answer
Reload manually in raycast? 1 Answer