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 xxbadxkarmaxx13 · Jul 25, 2013 at 03:27 AM · javascriptmathinventorytriggers

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

  
Comment
Add comment · Show 1
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 cdrandin · Jul 25, 2013 at 03:34 AM 0
Share

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.

1 Reply

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

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);
         }
     }
 }
Comment
Add comment · Show 3 · 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
avatar image xxbadxkarmaxx13 · Jul 25, 2013 at 05:04 AM 0
Share

I cant thank you enough :D Yes I know, my organization is terrible :/

avatar image xxbadxkarmaxx13 · Jul 25, 2013 at 05:08 AM 0
Share

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

avatar image tw1st3d · Jul 25, 2013 at 06:23 PM 0
Share

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

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

16 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

Related Questions

inventory with equip 1 Answer

How can i store the Amount of an item i have in an inventory? 0 Answers

Javascript syntax errors with basic expressions 2 Answers

Help with Inventory Script 1 Answer

Item Icon goes to origin, not set transform. (JAVASCRIPT) 0 Answers


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