- Home /
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..
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..
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.
ah i see. thank you for your help it works perfectly now
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.
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?
the player has a character controller and the item has a box collider and ive tried with and without having isTrigger enabled..
Are you sure it's on the right layer? Raycasting is ignored for objects on certain layers.
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?)