Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Therian13 · Jan 18, 2014 at 03:54 AM · inventoryreloadammo

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. :)

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Jan 18, 2014 at 04:01 AM 0
Share

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)

avatar image Therian13 · Jan 18, 2014 at 04:23 AM 0
Share

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");
     }
 }
 
 
avatar image Therian13 · Jan 18, 2014 at 04:25 AM 0
Share

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?

avatar image getyour411 · Jan 18, 2014 at 04:30 AM 0
Share

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.

avatar image Therian13 · Jan 18, 2014 at 04:44 AM 0
Share

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

}

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges