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 Xetros · Oct 26, 2014 at 09:18 PM · inventoryitem

Problem with picking up objects using a raycast and placing them in the inventory.

Hi there,

So I made two scripts both attached to a different gameobject. I tried to get an input from the user and then draw a line to the item. If the item would be in a certain range i'd add the item to the inventory and then destroy the item from the world so it would be placed inside my inventory.

The problem is that whenever I walk up to an item and pick it up every other item is automaticlly also picked up. But I only want it to pick up that one item I was looking at.

This is my script:

 #pragma strict
 
 private var _inventoryScript : Inventory;
 private var _player : GameObject;
 
 var maxDistance : float = 2;
 
 function Start()
 {
     _player = GameObject.FindGameObjectWithTag("Player");
     _inventoryScript = _player.GetComponent(Inventory);
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.E))
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
         
         if(Physics.Raycast(ray, hit, maxDistance))
         {
             if(hit.collider.gameObject.tag == "Item")
             {
                 _inventoryScript.AddItem(1);
                 Destroy(gameObject);
             }        
         }
     }
 }    
     

This script is the same for every item all I did was change the number in the AddItem function when i'm calling it to a certain item ID.

How can I make it so that when I try to pick up the item it doesn't place all the other items in my inventory aswell?

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Oct 26, 2014 at 09:22 PM

Assuming this is on every item, you can change line 23 to:

  if (hit.transform == transform)

Note it would be better to only have this script on one game object. Then rather than the change I just outlined, you could change line 26 instead:

  Destroy(hit.transform.gameObject);

And therefore you would only have a single Raycast() rather than a Raycast() by every item in the scene.

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 Xetros · Oct 26, 2014 at 10:41 PM 0
Share

If i'd put it on one gameobject then how would I tell my _inventoryScript.AddItem function which item it should add to my inventory? I tried this before but I couldn't get it to work.

avatar image robertbu · Oct 26, 2014 at 11:58 PM 0
Share

Right now you are doing:

   _inventoryScript.AddItem(1);

So can I assume that '1' is the number for this specific item? So ins$$anonymous$$d you would have a script on each item (or you could use a tag or name), that indicates the item number.

  public var itemNo = 1;

You would set the item number in the inspector. A enum would be better. And this could would not have Raycast() code. Let's assume the script on the items is called 'Item'. Then the Raycasting code (which exists once and on an empty game object and not an item) would be something like:

  function Update()
  {
      if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
      {
          var hit : RaycastHit;
          var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
          
          if(Physics.Raycast(ray, hit, maxDistance))
          {
              if(hit.collider.gameObject.tag == "Item")
              {
                  int itemNo = hit.collider.GetComponent(Item).itemNo;
                  _inventoryScript.AddItem(itemNo);
                  Destroy(hit.transform.gameObject);
              }        
          }
      }
  }    
avatar image Xetros · Oct 27, 2014 at 08:06 AM 0
Share

Oh, okay thank you very much.

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

2 People are following this question.

avatar image avatar image

Related Questions

make inventory script get variables from item's script 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unable to usem item from inventory 1 Answer

Implementing an Item system - Defining Items 1 Answer

Array Help GameObject Length Inventory 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