Adding Objects to Inventory

Okay, so I’ve been designing an inventory system for the game I’m working on. Let’s begin with the code I’ve got working.

var InventoryFrame : Texture;
var GunFrame : Texture;
var InventoryXPos = 650;
var InventoryYPos = 150;
var OpenInventory : boolean = false;
var InventorySlot01 : Texture;
var InventorySlot02 : Texture;
var InventorySlot03 : Texture;
var InventorySlot04 : Texture;
var InventorySlot05 : Texture;
var InventorySlot06 : Texture;
var InventorySlot07 : Texture;
var InventorySlot08 : Texture;
var InventorySlot09 : Texture;
var isControllable : boolean = true;

function OnGUI(){
GUI.Label (Rect(20,20, 256, 113), GunFrame);
if (OpenInventory == true){
	GUI.Label(Rect(InventoryXPos,InventoryYPos, 256, 404), InventoryFrame);
	GUI.Label(Rect(InventoryXPos + 10, InventoryYPos + 18, 80, 80), InventorySlot01);
	GUI.Label(Rect(InventoryXPos + 92, InventoryYPos + 18, 80, 80), InventorySlot02);
	GUI.Label(Rect(InventoryXPos + 174, InventoryYPos + 18, 80, 80), InventorySlot03);
	GUI.Label(Rect(InventoryXPos + 10, InventoryYPos + 105, 80, 80), InventorySlot04);
	GUI.Label(Rect(InventoryXPos + 92, InventoryYPos + 105, 80, 80), InventorySlot05);
	GUI.Label(Rect(InventoryXPos + 174, InventoryYPos + 105, 80, 80), InventorySlot06);
	GUI.Label(Rect(InventoryXPos + 10, InventoryYPos + 189, 80, 80), InventorySlot07);
	GUI.Label(Rect(InventoryXPos + 92, InventoryYPos + 189, 80, 80), InventorySlot08);
	GUI.Label(Rect(InventoryXPos + 174, InventoryYPos + 189, 80, 80), InventorySlot09);

}

}

function Update()
{


	if (Input.GetKeyDown ("tab")){
		if (OpenInventory == true){
		//if the Inventory is already open, close it when the tab button is pressed
		OpenInventory = false;
		Screen.lockCursor = true;
		isControllable = true;
		}
		else if(OpenInventory == false){
		//if the inventory is closed, open it when the tab button is pressed
		OpenInventory = true;
		Screen.lockCursor = false;
		Screen.showCursor = true;
		isControllable = false;
		}
	}

The code above controls the inventory GUI. It’s a grid of 9 squares, each representing the items in your inventory. It is shown whenever the Tab key is pressed.

Now where I’m running into issues is here:

function Update(){

var ThePlayer; 
var Facing = transform.TransformDirection(Vector3.forward);
var ObjectHit : RaycastHit;

if(Physics.Raycast(transform.position, Facing, ObjectHit, 2.0)){
}

}

This is the script that basically translates the 3d object from 3d into a 2d GUI item. Basically, what I’m trying to do, is have the raycast hit a collider, then I need to be able to check if that collider belongs to an Item that can be picked up, then destroy that object in the 3d world, and add the icon to the inventory.

How do I go about doing this? Any help would be appreciated

There are many topics about inventory systems all over the forum. Here is a real good one with some downloadable examples. Thereare a few examples from the showcase too. :smile:

http://forum.unity3d.com/viewtopic.php?t=11865

Its way better to use arrays to create a inventory system. as you can see from the examples in the link the way to go when adding an item is to deactivate it not destroy it :smile:

Mantis, I appreciate the help, but I’ve run into a few problems. First off, I’m a relatively novice scripter. The one I’ve posted here is probably the most complicated script I’ve put together so far on my own, and it’s not even that good.

I looked at the link you gave me for several hours, but the code used is really advanced, which makes it very difficult for me to modify it for my own uses. It’s like trying to read a college text book when you read at the second grade level. So, I’m gonna need someone to either break that script down for me into simpler terms, or answer the questions I posted in my first post.

Now, pertaining to my first post. The issue I have is, I know what I want to do and largely how to do it, but syntax stands in my way. How do I use a Raycast to see if I’ve hit a collider? Then once that’s done, how do I check to see what object that collider belongs to?

Thx for the help so far

From “Unity Game Development Essentials” :

var hit : RaycastHit;
if(Physics.Raycast (transform.position,
transform.forward, hit, 5)) {
if(hit.collider.gameObject.tag=="outpostDoor"
 doorIsOpen == false){
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
}
}

From the Raycast (hit), you get the collider (collider), the gameObject associated (gameObject). From there, you can either get the tag (tag) ou the name (name). Can it help you ?

That did help Akilae, it helped me to understand the relationship of the object and the collider.

Here’s the code I came up with, but I don’t think it’s correct still

function Update(){

var ThePlayer; 
var Facing = transform.TransformDirection(Vector3.forward);
var ObjectHit : RaycastHit;

if(Physics.Raycast(transform.position, Facing, ObjectHit, 2.0)){
}
if(ObjectHit.collider.gameObject.name == "ITEM_Flashlight"){
print("I collided with the flashlight!");
}
}

I think, the problem is that the code is saying that collider is the parent of GameObject, but in reality, GameObject is the parent of collider. However, GameObject cannot be a child of RaycastHit. So then, how would I go about writing this?

Still need help lol

You’ll want to access the transform of the object you hit, and the parent of that transform, and the GameObject tied to that transform. So the line will look like

  if(ObjectHit.transform.parent.gameObject.name == "ITEM_Flashlight")

Though you may be better off using a tag? Or some other attribute, since names are easy to modify. As for making an inventory system… it requires a strong knowledge of inheritance and polymorphism, and a keen organizational sense. You’re basically building a very complex data exchange system. I didn’t attempt it until I had two years of Unity coding under my belt. The finished product is ~2008 lines long. You can see it in the distro. Info at this link:

My http://forum.unity3d.com/viewtopic.php?t=55243

My advice: find something easier to do, or else take it very slow and thoughtfully.

Im tellin ya arrays is the way to go, short and sweet. But the big problem is saving your inventory, when saving your game. What are you saving your game state with playerPrefs or what?