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 thornekey · Mar 16, 2014 at 11:24 AM · c#arraylistinventory system

Inventory System wont work

Hi, im trying to make an inventory system, and its not working. Here is the code

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MyInventory : MonoBehaviour {
     
     public List<InvItems> Inventory;
     public InvItems[] Bag;
     
     private Rect windowRect = new Rect(0,0,150,350);
     private RaycastHit hit;
     private Ray ray;
     private Vector3 pos;
     // Use this for initialization
     void Awake () {
         Inventory = new List<InvItems>();
         
     }
     void Update () {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Input.GetButtonDown("Fire1")) {
             if (Physics.Raycast(ray,out hit,2)) {
                 Debug.Log("an item was picked up");
                 for(int i = 0;i < Bag.Length;i++) {
 
                         if(hit.collider.tag == Bag[i].id.ToString()) {
                             Inventory.Add(Bag[i]);
                             Destroy(hit.collider.gameObject);
                     }
                 }
             }
         }
         pos = transform.position;
         pos.x += 2;
     }
     void OnGUI (){
         windowRect = GUI.Window(0,windowRect,winwindow, "Inventory");
     }
     
     void winwindow(int windowID) {
         int y = 20;
             
         GUI.DragWindow(new Rect(0,0,600,32));
         
             for (int i = 0;i < Inventory.Count;i++) {
                 if(GUI.Button(new Rect(20,y,32,32), Inventory[i].icon)) {
                     Instantiate(Inventory[i].player,pos,transform.rotation);
                     Inventory.RemoveAt(i);
                 }
                 GUI.Label(new Rect(20,y+40,32,20), Inventory[i].name);
                 GUI.Label(new Rect(58,y,100,36), Inventory[i].description);
 
                 y += 70;
         }
     }
 }

i believe the error is occuring around here

 if (Input.GetButtonDown("Fire1")) {
                 if (Physics.Raycast(ray,out hit,2)) {

if i put a debug on either side of the second if statement the first one goes but not the second. Im not sure why. It was working another day, i must have changed somethign..

Comment
Add comment · Show 4
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 thornekey · Mar 16, 2014 at 01:13 PM 0
Share

oh, that has done it. Why does it have to be for the camera? The script is on the player.. and the camera is about 10 units away..

avatar image pako · Mar 16, 2014 at 01:19 PM 1
Share

You define ray in line 20 of your code as:

ray = Camera.main.ScreenPointToRay(Input.mousePosition);

This creates a ray from a Screen Point where the mouse clicks on. This Screen Point lies on the near plane of the camera (if my memory serves me well). Therefore, it doesn't matter if the script is attached on the player, the ray is always cast from the camera.

avatar image thornekey · Mar 16, 2014 at 01:21 PM 0
Share

ah i see. thank you for your help it works perfectly now

avatar image thornekey · Mar 16, 2014 at 01:40 PM 0
Share

thanks got it. i accepted the wrong one :)

2 Replies

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

Answer by pako · Mar 16, 2014 at 01:10 PM

A value of 2 seems very small for distance. Is your camera so close to what you're picking up? Why don't you try a large value for distance e.g 2,000 or even 20,000, to see if that makes a difference.

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
avatar image
0

Answer by wildex999 · Mar 16, 2014 at 12:24 PM

The only thing I can think of it that you're either missing a collider on the target object, or maybe the distance is wrong?

However, why don't you just put a OnMouseOver on the inventory item, and check for Input.GetMouseButtonDown inside it?

Comment
Add comment · Show 5 · 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 thornekey · Mar 16, 2014 at 12:29 PM 0
Share

the player has a character controller and the item has a box collider and ive tried with and without having isTrigger enabled..

avatar image wildex999 · Mar 16, 2014 at 12:33 PM 0
Share

Are you sure it's on the right layer? Raycasting is ignored for objects on certain layers.

avatar image thornekey · Mar 16, 2014 at 12:35 PM 0
Share

theyre all on the 'default' layer

avatar image wildex999 · Mar 16, 2014 at 12:37 PM 0
Share

Then I don't know, sorry =(

Just try debugging the values you're sending in to see if there are strange values. Change distance for the ray(Is the ray maybe being cast "behind" the object? I.e, what is the z position of the object?)

avatar image thornekey · Mar 16, 2014 at 12:49 PM 0
Share

how do i do that?

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

22 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 avatar image avatar image avatar image

Related Questions

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

Give a name for each class in List/array 2 Answers

Splitting text from lines into variables 0 Answers

Pathfinding through pairs of connections 2 Answers

Filling array with Scribtable Objects automaticly? 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